Script help!! to compare 2 JSON string
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2022 03:43 AM
Hello Guys,
I am not that good in scripting except basics and eventually learning scripting
Can anyone please help me with a sample script to compare 2 JSON strings ( we have the MRV's data stored in the Custom table, whenever the user resubmits the same request they will edit, add or delete the data and we need to Update the corresponding record in the Custom table)
Note: they both have same fields
ex: Custom table data stored
Custom Table data ::[ {
"vendor" : "Microsoft",
"model" : "v1",
"rack_unit" : "metre",
"equipment_type" : "Colo"
}, {
"vendor" : "Dell",
"model" : "ACer",
"rack_unit" : "kilo",
"equipment_type" : "Flex"
} ]
while resubmitting the request the user might modify the MRV data as below
variableSetValue::[ {
"vendor" : "Microsoft",
"model" : "v1",
"rack_unit" : "metre",
"equipment_type" : "Colo1" // Update the custom table record
}, {
"vendor" : "Dell",
"model" : "ACer",
"rack_unit" : "kilo",
"equipment_type" : "Flex"
}, {
"vendor" : "1",
"model" : "1",
"rack_unit" : "1",
"equipment_type" : "1" // Insert new Row in the Custom table
} ]
similarly they might delete the record and the corresponding data needs to be deleted in the Custom table
Sample script is enough for me and i will try to enhance it by refering the sample
Thank you!!!
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2022 04:50 AM
Hi
You can do it easily with free online sites.
If you want to do it in code then just use for loop and you should get the difference.
var json1 = {"age":"45","address":"12/B","name":"John"};
var json2 = {"name":"John","age":"45","city":"Auckland"};
for (var i in json1) {
if (JSUtil.nil(json2[i]) || json2[i] != json1[i])) {
gs.info(i + ": " + json1[i] + " not found in JSON2");
}
}
for (var i in json2) {
if (JSUtil.nil(json1[i]) || json2[i] != json1[i])) {
gs.info(i + ": " + json2[i] + " not found in JSON1");
}
}
To learn more from here too: https://community.servicenow.com/community?id=community_question&sys_id=3adf1f85db19949023f4a345ca96...
Mark my answer correct & Helpful, if Applicable.
Thanks,
Sandeep

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2022 05:17 AM
Glad to see my answer helped you, Kindly mark the answer as Correct & Helpful both such that others can get help.
Thanks,
Sandeep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2022 05:42 AM
Hello Sandeep,
Yes i tried it and it doesnot work for me, Can you please provide a sample script
1)First JSON string is the data in the Custom table
2) Second JSON string is the data changed while resubmitting the request
I need a script to compare 2nd JSON with First JSON and if there is Update in the Object values update the corresponding custom table record
if in the 2nd JSON the new row is added then add the same data in the Custom table and similarly if the data is deleted then delete the corresponding records in the custom table
It will be very helpful for me if you share the sample script and i will try to enhance it