- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2022 12:30 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2022 03:03 AM
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])
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2022 12:57 AM
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();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2022 01:59 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2022 03:03 AM
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])
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2022 01:21 AM
hello @Vineela1 ,
Yes there is a type of field which you can use to store the JSON's.
Field type: Name-Value pairs
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