Pull data from JSON array and save it into string field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-17-2017 05:34 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-17-2017 11:00 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-17-2017 11:08 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-18-2017 05:21 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-18-2017 05:57 AM
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.
