- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2017 11:17 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2017 11:17 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2017 03:30 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2017 03:34 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-21-2018 02:40 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-21-2018 10:31 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-16-2019 05:19 AM
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);