How to get the JSON key and value. How to get the length of parent.

Raj82
Kilo Expert

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

 


}
}
}

1 ACCEPTED SOLUTION

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

View solution in original post

12 REPLIES 12

Hi Asifnoor,

 

I am not understanding. Can you please explain little bit more with example?

 

Thanks,

Rajesh

Hello Raj,

It is difficult to parse the json string that you have shared, as it contains the following strings soap:Envelope, soap:Body.

So from wherever you are getting response, check if there is a possibility to get only return separately as a json string.

Otherwise, you need to do string operations and get the return outside of this string and then parse it.

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

Hi Asifnoor,

 

Thanks for your response. I find the way. Please see my above response.

 

Thanks,

Raj