- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2020 03:33 AM
I have converted the XML into JSON. But I am unable to get any values from that Json. Also I am not able to get the length. Please help. I want to know how many info there and I want to get the key and value from each info node. I consider each info is single record.
{
"soap:Envelope": {
"xmlns:soap": "http://www.w3.org/2003/05/soap-envelope",
"soap:Body": {
"customerListResponse": {
"xmlns": "http://ei2.nobj.nable.com/",
"return": [
{
"info": [
{
"value": "622",
"key": "customer.customerid"
},
{
"value": "All Covered - Workspaces",
"key": "customer.customername"
}
]
},
{
"info": [
{
"value": "623",
"key": "customer.customerid"
},
{
"value": "1All Covered - Workspaces 1",
"key": "customer.customername"
}
]
}
]
}
}
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2020 12:10 AM
To get the length of Info:
var req = JSON_FILE;
var totalRecords = req["soap:Envelope"]["soap:Body"]["customerListResponse"]["return"].length;
To get the key and value in first "Info":
var key = req["soap:Envelope"]["soap:Body"]["customerListResponse"]["return"][0].info[0].key;
var value = req["soap:Envelope"]["soap:Body"]["customerListResponse"]["return"][0].info[0].value;
Thanks,
Raj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2020 07:01 AM
That's a really nasty JSON you're working with. You can get the details you need with some string manipulation to get the colons out and to replace 'return' with a non-reserved word but if you can change the source that'd be preferable.
var obj = {
"soap:Envelope": {
"xmlns:soap": "http://www.w3.org/2003/05/soap-envelope",
"soap:Body": {
"customerListResponse": {
"xmlns": "http://ei2.nobj.nable.com/",
"return": [{
"info": [{
"value": "622",
"key": "customer.customerid"
},
{
"value": "All Covered - Workspaces",
"key": "customer.customername"
}
]
},
{
"info": [{
"value": "623",
"key": "customer.customerid"
},
{
"value": "1All Covered - Workspaces 1",
"key": "customer.customername"
}
]
}
]
}
}
}
};
function replacer(match) {
return match[0] + match[2];
}
var objstring = JSON.stringify(obj).replace(/[a-z]:[a-z]/gmi, replacer).replace('return','returns');
var newJSON = JSON.parse(objstring);
var returnObj = newJSON.soapEnvelope.soapBody.customerListResponse.returns;
for (var i = 0; i < returnObj.length; i++) {
for (var j = 0; j < returnObj[i].info.length; j++) {
gs.log(returnObj[i].info[j].key + ' ' + returnObj[i].info[j].value);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2020 12:04 AM
Hey David,
Yes I know, this type of Json was very difficult to handle. I have converted the XML into JSON,that's why we get this. But I find the way to handle these type of difficult JSON. Please below.
To get the length of Info:
var req = JSON_FILE;
var totalRecords = req["soap:Envelope"]["soap:Body"]["customerListResponse"]["return"].length;
To get the key and value in first "Info":
var key = req["soap:Envelope"]["soap:Body"]["customerListResponse"]["return"][0].info[0].key;
var value = req["soap:Envelope"]["soap:Body"]["customerListResponse"]["return"][0].info[0].value;
Thanks,
Raj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2020 12:10 AM
To get the length of Info:
var req = JSON_FILE;
var totalRecords = req["soap:Envelope"]["soap:Body"]["customerListResponse"]["return"].length;
To get the key and value in first "Info":
var key = req["soap:Envelope"]["soap:Body"]["customerListResponse"]["return"][0].info[0].key;
var value = req["soap:Envelope"]["soap:Body"]["customerListResponse"]["return"][0].info[0].value;
Thanks,
Raj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2020 12:31 AM
that's much easier than my way 🙂

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2020 04:09 AM
Hello Raj,
As i see it, the return is enclosed within customerListResponse. Is it possible to get the return object only as a json, so that it is easier to read.
Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.
Regards,
Asif
2020 ServiceNow Community MVP