Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Pull data from JSON array and save it into string field

chandukollapart
Tera Contributor

Hi,

We are doing integration to 3rd party system using web services and data will be getting in JSON format. I can pull data from each field but i have an JSON array from the list. So, i want to save the array data to string field in service now.

Data:

{

"ServiceTeam": null,

"Software": null,

"SoftwareVersion": null,

"Source": "MDS",

"IPAddresses": [

{

"Name": null,

"Number": "45231",

"Description": "Intel Ethernet Connection",

"IPAddress": "xx:xx:xx:xx:xx:xx"

},

{

"Name": null,

"Number": "45232",

"Description": "Async Adapter",

"IPAddress": "xx:xx:xx:xx:xx:xx"

},

{

"Name": null,

"Number": "45233",

"Description": "Intel Dual Band Wireless",

"IPAddress": "xx:xx:xx:xx:xx:xx"

}

]

},

In this above example i want to save the data from "IPAddresses": [ to end of an array in the string field. Any suggetions

Thanks

8 REPLIES 8

divya mishra
Tera Guru

Hi Chandu,


Try this



var myData=



{


"ServiceTeam":null ,


"Software": null,


"SoftwareVersion":null ,


"Source": "MDS",


"IPAddresses":[]


}




Then inorder to push the data to IP Address use this :


myData.IPAddresses.push({


"Name": null,


"Number": "45231",


"Description": "Intel Ethernet Connection",


"IPAddress": "xx:xx:xx:xx:xx:xx"


});




myData.IPAddresses.push({


"Name": null,


"Number": "45232",


"Description": "Async Adapter",


"IPAddress": "xx:xx:xx:xx:xx:xx"


});




myData.IPAddresses.push({


"Name": null,


"Number": "45233",


"Description": "Intel Dual Band Wireless",


"IPAddress": "xx:xx:xx:xx:xx:xx"


});






post me your feedback


Please Hit ✅Correct, ��Helpful, or ��Like depending on the impact of the response


Have a lovely day ahead




Regards,


Divya Mishra


divya mishra
Tera Guru

To Fetch IP Address :



Data.IPAddresses[0].IPAddress;



post me your feedback


Please Hit ✅Correct, ��Helpful, or ��Like depending on the impact of the response


Have a lovely day ahead


Hi Divya,



This is the code i am using it. Please let me know if there is any changes



Code cms.JPG


Output:


Address1.JPG



Thanks


Hello,




The response from above is a nested json, which is inside the array, am I right?




you might want to try this:




//parse the main response first


var response = JSON.parse(responseBody);


//access the sub part


for(var ctr = 0; ctr < response.IPAddresses.length; ctr++)


{


//parse each record on array


//note that each iteration of the for loop is a record in the IPAddresses array


var subParser = JSON.parse(response.IPAddresses[ctr]);


//try accessing the parts


gs.log("Access IP Address " + subParser.IPAddress);




}




hope this helps.




a.c.manlangit


ServiceNow Developer - Philippines




Mark like, helpful, or correct answer depending on the impact.