JSON.stringfy() not working on Background script

Aruna Sree Yela
Tera Guru

Hi,

 

JSON.stringfy() was working if I directly pass JSON object to a variable. If I try it with glide record that's not working. 

 

For your ref PFA:

 

Scenario 1:

Passing JSON object directly to a variable:

var str = {"u_supply_planner_field_1":[{"original_value":"abc","changed_value":"ABC"}]};
var res = JSON.stringify(str);

gs.print(res);

var parsedData = JSON.parse(res);

gs.print(parsedData.u_supply_planner_field_1[0].original_value);

Output:

ArunaSreeYela_1-1708440982075.png

 

Scenario 2:

 

Gliding Record:

var caseR = new GlideRecord('sn_customerservice_case');
caseR.addEncodedQuery('state=10^number=CS0001031');
caseR.query();
if(caseR.next()){
gs.print(caseR.short_description);
var jsonObj = caseR.u_json_output;
gs.print(jsonObj);
var res = JSON.stringify(jsonObj);
gs.print(res);
var parsedData = JSON.parse(jsonObj);
gs.print(parsedData);
gs.print(parsedData.u_supply_planner_field_1[0].original_value);
}

 

Output:

ArunaSreeYela_2-1708441183570.png

 

 

Thanks in advance 🙂

2 ACCEPTED SOLUTIONS

saurabh_dubey
Kilo Sage

Hi @Aruna Sree Yela ,

 

Use this...

 

 

var jsonObj = {}; // declare empty object here...
var res = {} ; // declare your res here
var parsedData = {} // declare your objects here...
var caseR = new GlideRecord('sn_customerservice_case');
caseR.addEncodedQuery('state=10^number=CS0001031');
caseR.query();
if(caseR.next()){
gs.print(caseR.short_description);
jsonObj = caseR.u_json_output;
gs.print(jsonObj);
res = JSON.stringify(jsonObj);
gs.print(res);
parsedData = JSON.parse(jsonObj);
gs.print(parsedData);
gs.print(parsedData.u_supply_planner_field_1[0].original_value);
}

 

 

So whats happening here is the 

var jsonObj = caseR.u_json_output;

it is declaring the jsonObj again so try to declare it outside the if // Refer the above code...

 

If my solution helps you do hit the like button and accept the solution.

 

Thanks and Regards,

Saurabh Dubey. 

View solution in original post

palanikumar
Mega Sage

From the response I understand the data is store in table as stringified version. Also observerved that your string has additional semicolum at the end. So, your code should be below:

var caseR = new GlideRecord('sn_customerservice_case');
caseR.addEncodedQuery('state=10^number=CS0001031');
caseR.query();
if(caseR.next()){
    gs.print(caseR.short_description);
    var jsonStr = caseR.u_json_output.toString();
    gs.print(jsonStr);
    //var res = JSON.stringify(jsonObj);
    //gs.print(res);
    if(jsonStr.substring(jsonStr.length-1) == ";"){ // Check if the string has semicolon at the end
        jsonStr = jsonStr.slice(0, -1); // remove the last charactor
    }
    var parsedData = JSON.parse(jsonStr);
    gs.print(parsedData);
    gs.print(parsedData.u_supply_planner_field_1[0].original_value);
}
Thank you,
Palani

View solution in original post

7 REPLIES 7

saurabh_dubey
Kilo Sage

Hi @Aruna Sree Yela ,

 

Use this...

 

 

var jsonObj = {}; // declare empty object here...
var res = {} ; // declare your res here
var parsedData = {} // declare your objects here...
var caseR = new GlideRecord('sn_customerservice_case');
caseR.addEncodedQuery('state=10^number=CS0001031');
caseR.query();
if(caseR.next()){
gs.print(caseR.short_description);
jsonObj = caseR.u_json_output;
gs.print(jsonObj);
res = JSON.stringify(jsonObj);
gs.print(res);
parsedData = JSON.parse(jsonObj);
gs.print(parsedData);
gs.print(parsedData.u_supply_planner_field_1[0].original_value);
}

 

 

So whats happening here is the 

var jsonObj = caseR.u_json_output;

it is declaring the jsonObj again so try to declare it outside the if // Refer the above code...

 

If my solution helps you do hit the like button and accept the solution.

 

Thanks and Regards,

Saurabh Dubey. 

palanikumar
Mega Sage

From the response I understand the data is store in table as stringified version. Also observerved that your string has additional semicolum at the end. So, your code should be below:

var caseR = new GlideRecord('sn_customerservice_case');
caseR.addEncodedQuery('state=10^number=CS0001031');
caseR.query();
if(caseR.next()){
    gs.print(caseR.short_description);
    var jsonStr = caseR.u_json_output.toString();
    gs.print(jsonStr);
    //var res = JSON.stringify(jsonObj);
    //gs.print(res);
    if(jsonStr.substring(jsonStr.length-1) == ";"){ // Check if the string has semicolon at the end
        jsonStr = jsonStr.slice(0, -1); // remove the last charactor
    }
    var parsedData = JSON.parse(jsonStr);
    gs.print(parsedData);
    gs.print(parsedData.u_supply_planner_field_1[0].original_value);
}
Thank you,
Palani

Anil Nemmikanti
Giga Guru

Hi,

As per my understanding, the data getting returned in string format. Can you please check type of jsonObj. Please convert it Object and then try to stringify and parse. 

Please mark it as helpful. If my answer is correct.

saurabh_dubey
Kilo Sage

Hi @Aruna Sree Yela , This is the second thread I have seen from you regarding the Objects question one was for the workflow and another is this,

 

Do refer this video.

If you speak hindi : https://www.youtube.com/watch?v=ju5j7rfXXTE

If you speak English : https://www.youtube.com/watch?v=wI1CWzNtE-M

 

Please mark my solution helpful if it helps you

Thanks, 

Saurabh

In this video we will talk about JSON, JavaScript object literals and AJAX. Ill show you how to create JSON objects and arrays and also how to make an Ajax request to a .json file CODE: Code for this video http://traversymedia.com/downloads/json_sandbox.zip EDUONIX COURSES: Please use affiliate ...
► Source Code & Notes: https://archive.codewithharry.com/videos/web-development-in-hindi-62 ►This video is a part of this Complete Web Development in Hindi Course Playlist: https://www.youtube.com/playlist?list=PLu0W_9lII9agiCUZYRsvtGTXdxkzPyItg ►Click here to subscribe - ...