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

@Vineela1 so where are the getting the JSON form your third party ?

is it a REST message ? 

Hi Mohith,

yes through, Rest API Post method, Third party is posting the data at ServiceNow table in JSON format. 

@Vineela1 is it a scripted REST API ? if yes you can write your script in the REST script it self to update your record 

 

you just have to do a glide record and then update it as below

var gr = new GlideRecord('your_table');
gr.addQuery('field_name','value'); // your query
gr.query();

if (gr.next())
{
gr.your_String_field = JSON.stringify('your_json_Response');
gr.update();
}

 

Hi Mohith,

No its not scripted rest API.

 

using Rest API explorer configured.

@Vineela1 are you using any BR to trigger that REST Message using REST Message V2 API?