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

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi @s r1 

Can you please share the complete and valid JSON which needs to be parsed?

 

 


Thanks and Regards,

Saurabh Gupta

Aniket Chavan
Tera Sage
Tera Sage

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