- 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
‎01-16-2019 07:08 AM
Hi,
The sample json you have shown is an array and hence your script cannot get the number directly. use below and check
var responseBody = response.getBody();
var parser = new JSONParser();
var parsedData = parser.parse(responseBody);
gs.log(parsedData[0].number);
Mark Correct if this solves your issue and also mark 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
‎01-16-2019 08:16 AM
That worked perfectly!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2020 03:24 AM
Hi
I have a similar array like below from which I need to get the value of EOLProductID (which should be WS-C2960-8TC-L), can you please help me in getting its value from the array
var arr = {"PaginationResponseRecord":{"PageIndex":1,"LastIndex":1,"TotalRecords":1,"PageRecords":1},"EOXRecord":[{"EOLProductID":"WS-C2960-8TC-L","ProductIDDescription":"Catalyst 2960 8 10/100 + 1 T/SFP LAN Base Image","ProductBulletinNumber":"EOL9055","LinkToProductBulletinURL":"http://www.cisco.com/en/US/prod/collateral/switches/ps5718/ps6406/eol__C51-726504.html","EOXExternalAnnouncementDate":{"value":"2013-01-28","dateFormat":"YYYY-MM-DD"},"EndOfSaleDate":{"value":"2013-07-29","dateFormat":"YYYY-MM-DD"},"EndOfSWMaintenanceReleases":{"value":"2014-07-29","dateFormat":"YYYY-MM-DD"},"EndOfSecurityVulSupportDate":{"value":"","dateFormat":"YYYY-MM-DD"},"EndOfRoutineFailureAnalysisDate":{"value":"2014-07-29","dateFormat":"YYYY-MM-DD"},"EndOfServiceContractRenewal":{"value":"2017-10-24","dateFormat":"YYYY-MM-DD"},"LastDateOfSupport":{"value":"2018-07-31","dateFormat":"YYYY-MM-DD"},"EndOfSvcAttachDate":{"value":"2014-07-29","dateFormat":"YYYY-MM-DD"},"UpdatedTimeStamp":{"value":"2014-06-04","dateFormat":"YYYY-MM-DD"},"EOXMigrationDetails":{"PIDActiveFlag":"Y","MigrationInformation":"Catalyst 2960C Switch 8 FE, 2 x Dual Uplink, Lan Base","MigrationOption":"Enter PID(s)","MigrationProductId":"WS-C2960C-8TC-L","MigrationProductName":"","MigrationStrategy":"","MigrationProductInfoURL":"http://www.cisco.com/en/US/products/ps11527/Products_Sub_Category_Home.html"},"EOXInputType":"ShowEOXByPids","EOXInputValue":"WS-C2960-8TC-L "}]}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-03-2020 04:19 AM
Hi Kunal,
since this is quite old thread; I would encourage you to create new question and give the link here or mention my name in that thread
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
‎04-03-2020 04:37 AM
Hi Ankur,
Thanks for the suggestion - I have created a new question (https://community.servicenow.com/community?id=community_question&sys_id=a81df8c4dbc41c9814d6fb243996195b)