How to parse JSON object values in Business rule

Mrman
Tera Guru

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

 

@Ankur Bawiskar 

13 REPLIES 13

shivambitanwar
Tera Expert

can you add gs.addInfoMessage() or gs.log() to see whats coming in parse2  and parse2.field12332.value 

also try to add parse2.field12332.value.toString() 

sometimes it works.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

I used this json and it worked well

there should not be a comma after this

{
  "field12332": {
    "profile": "xyzrt",
    "id": 6,
    "value": "NY-tester-704-HXQ-NY211"
  }
}

var json = '{"field12332":{"profile":"xyzrt","id":6,"value":"NY-tester-704-HXQ-NY211"}}';

var parser = JSON.parse(json);

var value = parser.field12332.value;

gs.info(value);

Output:

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar My actual JSON response is mentioned below , earlier I pasted only part of it .

Please suggest how to get from here.

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

it would still work

Is that not working fine?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader