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

Hi Mohith,

Third party vendor will post the data to ServiceNow, after getting the record I created after BR on that table. when we receive a record , it will trigger. then try to copy the fields to another table fields.

 

Regards,

Vineela

@Vineela1 then i think in the same BR where you are trying to copy the fields to another table you can copy the JSON also to short desc using JSON.stringify()

 

But i am assuming you are also getting the response as JSON in the same BR?