How to parse array of objects which are in json.

swathigangadhar
Tera Expert

How to parse array of objects which are in json.?

I Have a response

[{"UserID":"10001","Name":"Ram"},

{"UserID":"10002","Name":"Sultana"},

{"UserID":"10003","Name":"Lakshmi"}]

As of now i have three records which i mentioned above, now how can i parse it? and add it into my 'User'(sys_user) table? which am running in background script.

1 ACCEPTED SOLUTION

Hi Swathi,



Copy and paste the response and store it in a string variable and execute this script in background and check length and whether values are fetched properly or not



var string = ""; // copy and paste your response in this variable


var parser = new JSONParser();


var parsedData = parser.parse(string);


var length = parsedData.length;


gs.print(length);


var userIDArray = [];


var nameArray = [];



for(var i=0;i<length;i++){


gs.print(parsedData[i].UserID);


gs.print(parsedData[i].Name);


userIDArray.push(parsedData[i].UserID.toString());


nameArray.push(parsedData[i].Name.toString());


}



gs.print('user id array is:'+userIDArray);


gs.print('name array is:'+nameArray);



Execute this script and share a snapshot of what you get in background



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

19 REPLIES 19

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Swathi,



Here is the code. I executed in background script and it gives values correctly



var string = '[{"UserID":"10001","Name":"Ram"},{"UserID":"10002","Name":"Sultana"},{"UserID":"10003","Name":"Lakshmi"}]';


var parser = new JSONParser();


var parsedData = parser.parse(string);


var length = parsedData.length;


gs.print(length);



for(var i=0;i<length;i++){


gs.print(parsedData[i].UserID);


gs.print(parsedData[i].Name);


}



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Swathi,



As per the code you can also push the values you are fetching in an array so that you can use the array later on



var string = '[{"UserID":"10001","Name":"Ram"},{"UserID":"10002","Name":"Sultana"},{"UserID":"10003","Name":"Lakshmi"}]';


var parser = new JSONParser();


var parsedData = parser.parse(string);


var length = parsedData.length;


gs.print(length);



var userIDArray = [];


var nameArray = [];



for(var i=0;i<length;i++){


gs.print(parsedData[i].UserID);


userIDArray.push(parsedData[i].UserID.toString());


nameArray.push(parsedData[i].Name.toString());


gs.print(parsedData[i].Name);


}



gs.print('user id array is:'+userIDArray);


gs.print('name array is:'+nameArray);



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

HI Ankur,



Thanks a lot for you reply...



But my actual requirement, to parse the response obtained after calling REST api.



so am running this code...


try {


var r = new sn_ws.RESTMessageV2('Test', 'Default GET');


var response = r.execute();


var responseBody = response.getBody();


var httpStatus = response.getStatusCode();


// gs.print(responseBody);



  var parser = new global.JSON();


  var parsed = parser.decode(responseBody);


gs.print(parsed.length);


var responsefinal = parsed.split('[')[1];


var userIDArray = [];


var nameArray = [];



for(var i=0;i<10;i++){


gs.print(responsefinal[i].UserID);


userIDArray.push(responsefinal[i].UserID.toString());


nameArray.push(responsefinal[i].Name.toString());


gs.print(responsefinal[i].Name);


}



}


catch(ex) {


var message = ex.getMessage();


}




but am getting all undefined values... where am doing wrong?


Hi Swathi,



The script which i provided gives you an idea how to parse the JSON object/response which you can try in background script.


It's not that this script will be replaced exactly in the place you are getting the response.


You need to add log statements to determine what is going wrong.



replace these line with below code and try



var parser = new global.JSON();


var parsed = parser.decode(responseBody);



to be replaced with



var parser = new JSONParser();


var parsed   = parser.parse(responseBody);



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader