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

Hi Ganpati,



When you are printing it use JSON.stringify();



gs.log("Details are:"+JSON.stringify(lis));



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 Swathi,



Can you mark the answer as correct, helpful and hit like if you were able to achieve your requirement?


This helps in removing this question from unanswered list and helps others to search it quickly.



Regards


Ankur


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

Shuvaday
Kilo Contributor

Hi Ankur

We are facing some problem in  receiving JSON data from JAR running in midserver.

When we are sending normal string I can see the value.But when the actual JSON string is passed,we are getting  Evaluation error.

 

This is our probe js:

jspr.setJavascript('var omecall = new Packages.com.practice.rest.Rest.Client(); omecall.getdetail(probe.getParameter("IP"),probe.getParameter("cred"),probe.getParameter("grp"),probe.getParameter("operation"));');

 

Please let us know where are we going wrong.

 

Thank you in advance.

Hi,

You need to debug it further. not sure why the error is coming.

If it is a json string you can parse it

Regards

Ankur

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

jboiteux
Kilo Expert

Hi Ankur,

I am facing a similar issue.  I am just not understanding how to get my response to be a string.  My response is in the below format.

"number":"INC0022682",
"short_description":"Short Desc. Test",
"description":"Desc. Test"

}

]

Code being used to receive response when not in array:


var responseBody = response.getBody();

parsed = new JSON.parse(responseBody);

gs.log(parsed.number);