How to parse JSON object values in Business rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2022 04:11 AM
Hi,
I am having following response which I need to parse and get the text in the 'value' key shown below . Please suggest .
I have tried parse2.field47191.value but it is not working . Getting error as 'com.glide.script.RhinoEcmaError: Cannot read property "value" from undefined'
I have tried this.
var parse2 = JSON.parse(responseBody);
var wvalue = parse2.field12332.value
{
"field12332":
{
"profile":"xyzrt",
"id":6,
"value":"NY-tester-704-HXQ-NY211"
},
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2022 12:11 AM
Thank you for marking my response as helpful.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2022 04:46 AM
Hi
If your main aim is to fetch the output of key "value" and the response format is going to be the same always then you can follow the code provided below:
var obj = "{\"firstname\":\"Max\",\"phones\":[{\"phonetype\":{\"id\":\"D83004\",\"formattedvalue\":\"Home\",\"value\":\"Home\"},\"entry\":118,\"phonenumber\":\"+1 720 435-0088\",\"primary\":true}],\"links\":[{\"rel\":\"self\",\"title\":\"The current profile being viewed.\",\"url\":\"https:\/\/api.test.com\"}],\"field12332\":{\"profile\":\"xyzrt\",\"id\":6,\"value\":\"NY-tester-704-HXQ-NY211\"},\"startdate\":\"2022-05-25\",\"email\":\"testcand0881@tester.com\",\"lastname\":\"Testmax\"}";
var jsonObj = JSON.parse(obj);
//Instead of above line you can add the object where you are storing JSON data
for (var key in jsonObj) {
if(typeof(jsonObj[key]) != 'object') {
if(key == 'value')
gs.print(jsonObj[key])
} else {
for( var key2 in jsonObj[key]) {
if(key2 == 'value') {
gs.print(jsonObj[key][key2]);
}
}
}
}
Please mark my answer as correct if this solves your issues!
If it helped you in any way then please mark helpful!
Thanks and regards,
Kartik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2022 04:51 AM
I am puling firstname , lastname, phonenumber ,startdate,email .
Along with these I need to pull 'value' under field12332.
I am able to get the remaining values except this 'value' . Please suggest if I need to use the code your provided for getting this ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2022 05:05 AM
Hi
Please find the updated code, though it is kind of static but will work for your requirement:
var obj = "{\"firstname\":\"Max\",\"phones\":[{\"phonetype\":{\"id\":\"D83004\",\"formattedvalue\":\"Home\",\"value\":\"Home\"},\"entry\":118,\"phonenumber\":\"+1 720 435-0088\",\"primary\":true}],\"links\":[{\"rel\":\"self\",\"title\":\"The current profile being viewed.\",\"url\":\"https:\/\/api.test.com\"}],\"field12332\":{\"profile\":\"xyzrt\",\"id\":6,\"value\":\"NY-tester-704-HXQ-NY211\"},\"startdate\":\"2022-05-25\",\"email\":\"testcand0881@tester.com\",\"lastname\":\"Testmax\"}";
var fn = '',
ln = '',
pn = '',
srtdt = '',
email = '',
value = '';
var jsonObj = JSON.parse(obj);
for (var key in jsonObj) {
if(typeof(jsonObj[key]) != 'object') {
if(key == 'firstname')
fn = jsonObj[key];
else if (key == 'lastname')
ln = jsonObj[key];
else if ( key == 'startdate')
srtdt = jsonObj[key];
else if (key == 'email')
email = jsonObj[key];
} else {
if (key == 'phones')
pn = jsonObj[key][0]['phonenumber'];
else if(key == 'field12332')
value = jsonObj[key]['value'];
}
}
gs.print(fn + '\n' + ln + '\n' + pn + '\n' + srtdt + '\n' + email + '\n' + value);
Please mark my answer as correct if this solves your issues!
If it helped you in any way then please mark helpful!
Thanks and regards,
Kartik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2022 08:40 AM
Hi
Did my reply answer your question?
If yes and if my answer helped you out then please mark my answer as correct/helpful, so that other community members facing similar issues might get help.
Thanks and regards,
Kartik