How to convert a gliderecord to JSON?

zsquared
Tera Contributor

Hello,

I am trying to pull a gliderecord from a table and insert it into an JSON object but for some reason my code is not working.

Any ideas?

getRollups();

function getRollups(){

  var myRollUps = [];

  var table = new GlideRecord('random_table');

  table.query();

  while (table.next()) {

  var allocation = 'Applications';

  var percentage = table.u_total_app_cost;

  myRollUps.push({

  'rollupKey': 'name',

  'rollupValue': allocation,

  'rollupAmount': percentage

  });

  }

  return myRollUps;

}

7 REPLIES 7

drjohnchun
Tera Guru

Hello Zhen,



You can use



var json = new global.JSON().encode(object);



to encode object to JSON. Hope this helps.



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile

visit snowaid


ServiceNow Advocate

My blog post GQL (Glide Query Language) Part 4: The Retriever might provide additional examples and ideas.


I replaced Line 10 with:



var json = new global.JSON().encode(table.u_total_app_cost);



and then changed Line 14 to equal json



Is this how it's supposed to be used?


Btw~ Thank you for all your help thus far!!!!!!!