Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

JSON.Parse is not working if We call that data from a field

Venkata Ramesh
Tera Contributor

Hi Experts,

I am placing the below JSON object in incident Short Description field and called that through GlideRecord in background script, then tried to parse that using JSON.parse once i received but it throwing syntax error

JSON object : {"name":"George,ramesh","lastname":"Washington"}

If i hardcode in variable and use JSON.parse then it is working fine, if I place this in field and call that then no function from JSON class is working

When i tried with JSON.stringify() i am getting emty bracess {}

 

Code

var inc = new GlideRecord('incident');
inc.addEncodedQuery('sys_id=9714a35a0f4a2f4067c6590be1050e63');
inc.query();
if(inc.next()){
var str = inc.description;
var obj = = JSON.parse(str);
gs.info(‘The first name is ’ + obj.name);
}

 

1 ACCEPTED SOLUTION

Alikutty A
Tera Sage

Both these script will work, I have tested.

var inc = new GlideRecord('incident');
inc.addEncodedQuery('sys_id=9714a35a0f4a2f4067c6590be1050e63');
inc.query();
if(inc.next()){
var str = inc.description;
var obj = JSON.parse(str);
gs.info(‘The first name is ’ + obj.name);
}

//OR

var inc = new GlideRecord('incident');
inc.addEncodedQuery('sys_id=9714a35a0f4a2f4067c6590be1050e63');
inc.query();
if(inc.next()){
var str = inc.description;
var obj = new JSON().decode(str);
gs.info(‘The first name is ’ + obj.name);
}

 

View solution in original post

3 REPLIES 3

Alikutty A
Tera Sage

Can you try this out once

var inc = new GlideRecord('incident');
inc.addEncodedQuery('sys_id=9714a35a0f4a2f4067c6590be1050e63');
inc.query();
if(inc.next()){
var str = inc.description.toString();
var obj = JSON.parse(str);
gs.info(‘The first name is ’ + obj.name);
}

Alikutty A
Tera Sage

There is a double equal sign on your JSON assignment.

var obj = = JSON.parse(str);

Correct it and it should work.

var obj = JSON.parse(str);

Alikutty A
Tera Sage

Both these script will work, I have tested.

var inc = new GlideRecord('incident');
inc.addEncodedQuery('sys_id=9714a35a0f4a2f4067c6590be1050e63');
inc.query();
if(inc.next()){
var str = inc.description;
var obj = JSON.parse(str);
gs.info(‘The first name is ’ + obj.name);
}

//OR

var inc = new GlideRecord('incident');
inc.addEncodedQuery('sys_id=9714a35a0f4a2f4067c6590be1050e63');
inc.query();
if(inc.next()){
var str = inc.description;
var obj = new JSON().decode(str);
gs.info(‘The first name is ’ + obj.name);
}