How to parse response body

s r1
Tera Contributor

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 

 

FieldValue
nameOracle
descriptionThis is used by db team.
Typedatabase
HostingSaas
lifeOperational
rel1problem
 incident
Ownerxyz@gmail.com 
otypeRESPONSIBLE
workDeveloper
6 REPLIES 6

Sandeep Rajput
Tera Patron
Tera Patron

@s r1 You can JSON.parse to parse a JSON string and convert into an object in ServiceNow.

 

var someObj = JSON.parse(<your json string>);
var glideCustom = new GlideRecord('custom table name');
glideCustom.initalize();
glideCustom.name=someObj.facts[0].cluster.name;
glideCustom.description=someObj.facts[0].cluster.description;
glideCustom.type=someObj.facts[0].cluster.description;
//Similarly do it for other fields
glideCustom.insert();

 

Fore more information on JSON.parse please refer to https://www.servicenow.com/community/developer-blog/json-parsing/ba-p/2279166

 

Hope this helps.

The response is below. It should be parsed from database till name. name value is Oracle which will be added in custom table

{
"database": {
"allSheets": {
"facts": [
{
"cluster": {
"name": "Oracle",
"description": "This is used by db team.",

Please refer to the JSON.parse method and you will be able to parse data in your desired format, refer to the examples provided on this post  https://www.servicenow.com/community/developer-blog/json-parsing/ba-p/2279166

Vishal Birajdar
Giga Sage

Hello @s r1 

 

Have simplified object (used some part) to get desired output.

You can try it in background script.

 

var r = {
    "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
                                    }
                                }
                            }
                        ]
                    }
                }
            }]
        }
    }
};

var clusterName = r.database.allSheets.facts[0].cluster.name;
var clusterDescription = r.database.allSheets.facts[0].cluster.description;


gs.print(clusterName);
gs.print(clusterDescription);




 

 

VishalBirajdar_0-1705851920014.png

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates