how to get exact string value without modifications in Javascript

Vineela1
Tera Contributor

Hi Team,

I have a requirement, from Third party client , I receive JSON format value. I want to copy to ServiceNow table which field type is string. then converting to JSON and copy to corresponding table.

Here I am facing challenge while copying JSON format value to string variable. It is getting changed the format.

 

Like example :

I will get JSON value from Third party as:

 

{"budget":"value","location":"value","jobid":"value"}

 

while getting to string value. It is like below format:

{budget=value,location=value,jobid=value}

 

Then I am unable to convert back to JSON. Please suggest do we have option to create a JSON variable in ServiceNow to store the json format value as it is.

 

Thanks and Regards,

 

Vineela

 

1 ACCEPTED SOLUTION

Store this value in the short description of any incident record.

{budget=value,location=value,plbs=value,u_custom_field1={loc={a=b,c={d=e}},plbs=value}}

 

Then use below in background script.

var grInc = new GlideRecord('incident');
grInc.get('<sys_id_of_that_incident_in_which_you_stored_above_value>');

var str = grInc.short_description.replace(/=/g, '":"');
str = str.replace(/,/g, '","');
str = str.replace(/{/g, '{"');
str = str.replace(/"{/g, '{');
str = str.replace(/}/g, '"}');
str = str.replace(/}"/g, '}');

gs.print(str);
var obj = JSON.parse(str);

printJSON(obj);

function printJSON(obj){
        for(var key in obj){
                if(typeof obj[key] == 'object'){
                        gs.print(key);
                        printJSON(obj[key]);
                }else{
                        gs.print(key + ': ' + obj[key])
                }
        }
}

 

View solution in original post

16 REPLIES 16

Muhammad Khan
Mega Sage
Mega Sage

Use JSON.stringify() method to store the json object in string field. Try below in background script.

var grInc = new GlideRecord('incident');
grInc.get('<any_open_incident_sys_id>');

var obj = {"budget":"value","location":"value","jobid":"value"};

grInc.short_description = JSON.stringify(obj);
grInc.update();

Hi Muhammad,

Thanks for sharing , it is working for this format, But my concern is , from third party, they are posting the data directly at ServiceNow table.

In JSON format, directly it is getting inserted to table. For that variable which stores ,is of type "String". So I need to fetch it from ServiceNow table.

Like example:

they posted this format:

{

"u_budget":"value",

"u_location":"value",

"u_plbs":"value"

}

In string variable it is storing as "{u_budget=value,u_location=value,u_plbs=value}

to convert it to JSON format getting issue. Please suggest on this.

 

Regards,

Vineela

Store this value in the short description of any incident record.

{budget=value,location=value,plbs=value,u_custom_field1={loc={a=b,c={d=e}},plbs=value}}

 

Then use below in background script.

var grInc = new GlideRecord('incident');
grInc.get('<sys_id_of_that_incident_in_which_you_stored_above_value>');

var str = grInc.short_description.replace(/=/g, '":"');
str = str.replace(/,/g, '","');
str = str.replace(/{/g, '{"');
str = str.replace(/"{/g, '{');
str = str.replace(/}/g, '"}');
str = str.replace(/}"/g, '}');

gs.print(str);
var obj = JSON.parse(str);

printJSON(obj);

function printJSON(obj){
        for(var key in obj){
                if(typeof obj[key] == 'object'){
                        gs.print(key);
                        printJSON(obj[key]);
                }else{
                        gs.print(key + ': ' + obj[key])
                }
        }
}

 

Mohith Devatte
Tera Sage
Tera Sage

hello @Vineela1 ,

Yes there is a type of field which you can use to store the JSON's.

Field type: Name-Value pairs

Screenshot 2022-11-14 at 14.47.55.png

But when you get the JSON from third party just use JSON.stringify('your json'); and then update the record with that stringified JSON.jUST DIRECTLY ASSIGN THE STRINGIFIED JSON to that name value pair field .

 

OR 

 

before updating the string field that you are using currently just stringify it and assign the JSON .

 

Hope this helps 

 

Mark the answer correct if this helps you 

Thanks