How to parse JSON - REST Integration

Neeta1
Tera Contributor

Hi experts ,

 

Could you please suggest how to parse below JSON code ?

{
"next_page_id":"789c5590d14ec3300c45dffd23dd0bd5d2756ce30ffa3221f601519aa425a88da3c485edefb12304e225528eaf7dafbd7349c1adc1ec69cb514fc12faee8155d6920755c69a0dc9a95eb3a99d90b3dc0b057424d9ee5dfc3cea5639599bbce55f30c4325d2a3439c90d909acc5f460c50cfc588c85f2660933a433ecacc8c605ef2d46df8ea67878e5ee8129a40b58adc72d2c14a2d680e387b70449ede14a6f492989a024ee84f9cb64c776ea376721934948cf44c012d650c1f127e68c843a610914306a9ca6e26b9dd778aa239c2123e0548dce5046869c821ec90bbfb0b5ac94ddcb3b16d2217df6c6b92c47ac96ff0f2c58c155b26de31fea647a7710fd147291089d64e609ed372a798519-f98dbcf9ca98b1bfb3598d29248b808440aca93db7cb38f9f1526c5",
"result":[
{
"_ref":"record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQuY29tLm50cnMudXAxNDM1MC4xMC4zMi4xNS40Ny4:10.32.15.47/up14350.ntrs.com/Internal",
"configure_for_dhcp":false,
"host":"up14",
"ipv4addr":"10.32.15.475"
},
{
"_ref":"record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQuY29tLm50cnMudXAxNDM0OS4xMC4zMi4xNS40Ni4:10.32.15.46/up14349.ntrs.com/Internal",
"configure_for_dhcp":false,
"host":"up1",
"ipv4addr":"10.32.15.4"
},
{
"_ref":"record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQuY29tLm50cnMudXAxNDI4NC4xMC4zMi4xNS4zMC4:10.32.15.30/up14284.ntrs.com/Internal",
"configure_for_dhcp":false,
"host":"up14",
"ipv4addr":"10.32.15.3"
},
{
"_ref":"record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQuY29tLm50cnMudXQxNTk5NC4xMC4zNC4xODUuMjE1Lg:10.34.185.215/ut15994.ntrs.com/Internal",
"configure_for_dhcp":false,
"host":"ut15.",
"ipv4addr":"10.34.185.2"
},
{
"_ref":"record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQuY29tLm50cnMudXQxNTk3OS4xMC4zMi4xNi40My4:10.32.16.43/ut15979.ntrs.com/Internal",
"configure_for_dhcp":false,
"host":"ut1597",
"ipv4addr":"10.32.16.43"
}
]
}

1 ACCEPTED SOLUTION

Try this @Neeta1 ,

var responseObj = JSON.parse(inputs.step_input_response_body);
    outputs.next_page_id = responseObj.next_page_id;
    outputs.ipaddress = [];
    outputs.dhcp = [];
    outputs.host = [];
    for (var i = 0; i < responseObj.result.length; i++) {
        outputs.ipaddress.push(responseObj.result[i].ipv4addr);
        outputs.dhcp.push(responseObj.result[i].configure_for_dhcp);
        outputs.host.push(responseObj.result[i].host);
    }

View solution in original post

5 REPLIES 5

Jaspal Singh
Mega Patron
Mega Patron

Hi Neeta,

Check for article that has possible use cases covered.

Prahlad Kumar
Tera Guru

Hi @Neeta1,

 

Please read the below points, how we can parse the JSON data:

 

PrahladKumar_0-1698821557272.png

 

Please mark Helpful / Accept Solution so that it helps others with similar questions.

 

Thanks & Regards

Prahlad Kumar
Tera Expert

LinkedIn - https://www.linkedin.com/in/prahlad-kumar-92a877117/

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Neeta1 

what you want to extract from it and what's the issue faced?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

 

Want to fetch host name and IP address , however I am able to pulled up  only 1st row data only.

How to go in to the loop ?

 

Please check the below code

 

(function execute(inputs, outputs) {

var responseObj = JSON.parse(inputs.step_input_response_body);

var itemsArray = responseObj;



for (var i = 0; i < 5; i++) {

outputs.next_page_id = responseObj.next_page_id;

outputs.ipaddress = responseObj.result[0].ipv4addr[i];

outputs.dhcp = responseObj.result[0].configure_for_dhcp[i];

outputs.host = responseObj.result[0].host[i];

}

}

 

Could you please suggest.

Thanks