How can we parse nested JSON

Ankita9793
Tera Contributor

Hi All,

 

How can we parse nested JSON? I tried it as below, it shows '[object Object],[object Object]'

 

 var request = new sn_ws.RESTMessageV2(restMessage, restMethod);
        for (var p in params) {
            if (p == "returnObj"){

                request.setStringParameterNoEscape(p, JSON.stringify(params[p][0]));
            }
            gs.info('params[p]' + JSON.stringify(params[p]) +JSON.stringify(p));
            request.setStringParameterNoEscape(p, params[p]);

        }
 Ankita9793_1-1752498999688.png

JSON DATA :

{"returnObj":[{"RequesterUserId":"45333530","RequestCategory":"01","ServiceNowFormNumber":"RITM1234","ScTaskNumber":"SCNum01234","AdminSystemName":"MDM","CustomerNumber":"042721608","KycRm":"072"},{"RequesterUserId":"45333530","RequestCategory":"01","ServiceNowFormNumber":"RITM1234","ScTaskNumber":"SCNum01267","AdminSystemName":"MDM","CustomerNumber":"350802601","KycRm":"610"}]}

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Ankita9793 

try this if you want to send the entire nested JSON as a string parameter:

request.setStringParameterNoEscape("returnObj", JSON.stringify(params["returnObj"]));

try this If you want to send the whole payload as the request body

request.setRequestBody(JSON.stringify(params));

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Ankita9793 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Bhimashankar H
Mega Sage

Hi @Ankita9793 ,

 

I think you want to parse the data from JSON. So I have tried below snippet to parse data.

 

var jsonData = {
    "returnObj": [{
        "RequesterUserId": "45333530",
        "RequestCategory": "01",
        "ServiceNowFormNumber": "RITM1234",
        "ScTaskNumber": "SCNum01234",
        "AdminSystemName": "MDM",
        "CustomerNumber": "042721608",
        "KycRm": "072"
    }, {
        "RequesterUserId": "45333530",
        "RequestCategory": "01",
        "ServiceNowFormNumber": "RITM1234",
        "ScTaskNumber": "SCNum01267",
        "AdminSystemName": "MDM",
        "CustomerNumber": "350802601",
        "KycRm": "610"
    }]
}
gs.info(jsonData.returnObj[1].RequesterUserId)

so here it will print the 

45333530

 One thing to consider is that 'returnObj' is the array and each one is array data and we can access those values using "." then property name.

 

Thanks,

Bhimashankar

----------------------------------------------------------------------------------------
Please mark my answer as helpful/correct if it resolves your query.
----------------------------------------------------------------------------------------

Bhimashankar H
Mega Sage

Hey @Ankita9793 ,

 

I hope you saw my reply.

----------------------------------------------------------------------------------------
If my answer helps you or resolves your query, please consider marking as 'Accept As Solution'. So future readers with similar questions can find it easily. Your coordination will help community to grow stronger!.
----------------------------------------------------------------------------------------

 

Thanks,
Bhimashankar H