The CreatorCon Call for Content is officially open! Get started here.

Loop through JSON on Client Side

suttonj
Kilo Explorer

Does anyone know of an efficient way of looping through JSON on the client side? My JSON looks like this:

{

    "results":[

        {

            "ticket_Number":"INC123456",

            "short_description":"this is a the short description"

        }

    ]

}

The above is currently being returned from a script include. I am calling the script include on the client side and storing the JSON in a variable like below:

var status = response.responseXML.documentElement.getAttribute("answer");

The variable status will hold the JSON I listed above. I am basically wanting to be able to grab the data like:

data.ticket_Number

data.short_description

Thanks!

4 REPLIES 4

kristenankeny
Tera Guru

We have something similar. At the end of the glideajax, we do ga.getXMLAnswer(callbackFunction); Then in the callback function it looks like:



function callbackFunction(user){


user = user.evalJSON();


//at this point we can start referencing the data such as user.u_location


}


Kristen,



Did you do anything special in your script include?



My return looks something similar to this:



var response = request.execute();


var responseBody = response.getBody();



return responseBody;




Thanks!


Our script include builds the return like this:



var results = {};


var getInfo = new GlideRecord('table_name');


getInfo.query();


results.location = getInfo.getValue('location');


...


return new JSON().encode(results);


adityaghosh
Tera Guru

this e.g. might help you...




sample json extracting various elements in json obj, for debugging/compiling use Snow -> Scripts - Background




var responseBody = '[{"number":0,"fields":[{"displayField":"fname","searchFields":"name","label":"Department1","type":"reference","valueField":"sys_id","value":{"value":"221f3db5c6112284009f4becd3039cc9","displayValue":"Development"},"table":"cmn_department1"},{"Dep":"Account Number","type":"text","value":"asdf"},{"min":0,"max":100,"label":"Percentage","type":"number","value":1},{"label":"test4","type":"text","value":"asdfadsf"}]},{"number":14,"fields":[{"displayField":"lname","searchFields":"name","label":"Department2","type":"reference","valueField":"sys_id","value":{"value":"a581ab703710200044e0bfc8bcbe5de8","displayValue":"Finance"},"table":"cmn_department2"},{"Dep":"SC Number","type":"text","value":"asdfadsfadf"},{"min":0,"max":100,"label":"Percentage","type":"number","value":3},{"label":"test4","type":"text","value":"asdfasdfadsf"}]}]';          
               
var obj = JSON.parse(responseBody);    


                var objLen = obj.length;      
  gs.print(i+ ' objLen :'+objLen );    
                gs.print('-----------------------------');  
                gs.print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');  
       
for (var i = 0, j = obj.length; i < j; i += 1) {      


                      var fieldsLen = obj[i].fields.length;      
        gs.print( '-> fieldsLen :'+fieldsLen );    
                      gs.print('-----------------------------');                      


                      var num = obj[i].number;      
        gs.print(i+ ' num :'+num );    
                      gs.print('');  
                     
                      var fields = obj[i].fields[0].displayField;
                      gs.print(i+ ' fields:'+fields);        
                      gs.print('');  


                      var value= obj[i].fields[0].value.value;
                      gs.print(i+ ' value:'+value);        
                      gs.print('');  


                      var table= obj[i].fields[0].table;
                      gs.print(i+ ' table:'+table);        
                      gs.print('');  

                      var dep= obj[i].fields[1].Dep;
                      gs.print(i+ ' dep:'+dep);        
                      gs.print('');  
                             
                    gs.print('-------------------------------------------');  


                             
}