How to parse response body
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2024 06:16 AM
Response body from third party tool to ServiceNow:
{
"database": {
"allSheets": {
"facts": [
{
"cluster": {
"name": "Oracle",
"description": "This is used by db team.",
"Type": "database",
"Hosting": "Saas",
"life": {
"state": "Operational"
},
"rel1": {
"totalCount": 4,
"edges": [
{
"node": {
"Sheet": {
"name1": "problem",
"description": null
}
}
},
{
"node": {
"Sheet": {
"name1": "incident,
"description": null
}
}
},
{
"node": {
"Sheet": {
"name1": "Supply chain",
"description": null
}
}
},
{
"node": {
"Sheet": {
"name1": "inovation",
"description": null
}
}
}
]
},
"Owner": {
"totalCount": 2,
"edges": [
{
"node": {
"user": {
"userName": "abc@gmail.com"
},
"type": "OBSERVER",
"roles": [
{
"name": "Architect"
}
]
}
},
{
"node": {
"user": {
"userName": "xyz@gmail.com"
},
"otype": "RESPONSIBLE",
"roles": [
{
"name": "Developer"
}
]
}
}
]
}
}
},
{
"cluster": {
"name": "Adobe",
"description": "Adobe.",
"Type": "database",
"Hosting": "Saas",
"life": {
"state": "Operational"
},
"subscriptions": {
"totalCount": 0,
"edges": []
}
}
},
above response to be parsed and each record to be added in ServiceNow Custom table
Field | Value |
name | Oracle |
description | This is used by db team. |
Type | database |
Hosting | Saas |
life | Operational |
rel1 | problem |
incident | |
Owner | xyz@gmail.com |
otype | RESPONSIBLE |
work | Developer |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2024 07:50 AM
Hi @s r1
Can you please share the complete and valid JSON which needs to be parsed?
Thanks and Regards,
Saurabh Gupta
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2024 07:59 AM
Hello @s r1 ,
You can give a try to the script below and let me know your views on this.
// Assuming your response is stored in a variable named 'responseBody'
var responseBody = {
"database": {
"allSheets": {
"facts": [
{
"cluster": {
"name": "Oracle",
"description": "This is used by db team.",
"Type": "database",
"Hosting": "Saas",
"life": {
"state": "Operational"
},
"rel1": {
"totalCount": 4,
"edges": [
{
"node": {
"Sheet": {
"name1": "problem",
"description": null
}
}
},
{
"node": {
"Sheet": {
"name1": "incident",
"description": null
}
}
},
{
"node": {
"Sheet": {
"name1": "Supply chain",
"description": null
}
}
},
{
"node": {
"Sheet": {
"name1": "inovation",
"description": null
}
}
}
]
},
"Owner": {
"totalCount": 2,
"edges": [
{
"node": {
"user": {
"userName": "abc@gmail.com"
},
"type": "OBSERVER",
"roles": [
{
"name": "Architect"
}
]
}
},
{
"node": {
"user": {
"userName": "xyz@gmail.com"
},
"otype": "RESPONSIBLE",
"roles": [
{
"name": "Developer"
}
]
}
}
]
}
}
},
{
"cluster": {
"name": "Adobe",
"description": "Adobe.",
"Type": "database",
"Hosting": "Saas",
"life": {
"state": "Operational"
},
"subscriptions": {
"totalCount": 0,
"edges": []
}
}
}
]
}
}
};
// Assuming your custom table name is 'your_custom_table'
var glideCustom = new GlideRecord('your_custom_table');
glideCustom.initialize();
var facts = responseBody.database.allSheets.facts;
for (var i = 0; i < facts.length; i++) {
var cluster = facts[i].cluster;
glideCustom.name = cluster.name;
glideCustom.description = cluster.description;
glideCustom.type = cluster.Type;
glideCustom.hosting = cluster.Hosting;
// Add other fields as needed
glideCustom.insert();
}
gs.print('Records added to the custom table successfully.');
Let me know your views on this and Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks,
Aniket