- 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 02:46 AM
@Vineela1 so where are the getting the JSON form your third party ?
is it a REST message ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2022 02:49 AM
Hi Mohith,
yes through, Rest API Post method, Third party is posting the data at ServiceNow table in JSON format.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2022 02:54 AM
@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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2022 02:58 AM
Hi Mohith,
No its not scripted rest API.
using Rest API explorer configured.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2022 03:00 AM
@Vineela1 are you using any BR to trigger that REST Message using REST Message V2 API?