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,

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

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

That worked perfectly!

kunal16
Tera Expert

Hi @Ankur Bawiskar  

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 "}]}

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

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

Hi Ankur,

Thanks for the suggestion - I have created a new question (https://community.servicenow.com/community?id=community_question&sys_id=a81df8c4dbc41c9814d6fb243996195b)