Parsing nested request through scripted rest API

Saquib Mohammed
Mega Guru

Hi,

I am trying to get to the nested element in json request using scripted rest api. Can someone guide me how to get to retrieve the value of Type and Name in the below sample nested json request?

{
"CorrelationId":"4009024",
"attach_details" : [
{"Type" : "text", "Name": "abc"},
{"Type" : "text", "Name": "xyz"}
]
}

Getting the CorrelationId is straight forward. This is my script for getting correlation id

var reqbody = request.body.dataString;
var parser = new global.JSON();
var parsedData = parser.decode(reqbody);
var correlationId = parsedData['CorrelationId'];

1 ACCEPTED SOLUTION

ricker
Tera Guru

@Saquib Mohammed,

Give this a shot.  You need get into the attach_details object and loop through each item....You might need to run JSON.parse(detailsArray); before the for loop.

{
"CorrelationId":"4009024",
"attach_details" : [
{"Type" : "text", "Name": "abc"},
{"Type" : "text", "Name": "xyz"}
]
}
//Getting the CorrelationId is straight forward. This is my script for getting correlation id
var reqbody = request.body.dataString;
var parser = new global.JSON();
var parsedData = parser.decode(reqbody);
var correlationId = parsedData['CorrelationId'];
var detailsArray = parsedData['attach_details'];
for (i=0; i < detailsArray.length; i++) {
      gs.info("Type: " + detailsArray[i].Type
      + "\nName: " + detailsArray[i].Name);
}

 

View solution in original post

2 REPLIES 2

ricker
Tera Guru

@Saquib Mohammed,

Give this a shot.  You need get into the attach_details object and loop through each item....You might need to run JSON.parse(detailsArray); before the for loop.

{
"CorrelationId":"4009024",
"attach_details" : [
{"Type" : "text", "Name": "abc"},
{"Type" : "text", "Name": "xyz"}
]
}
//Getting the CorrelationId is straight forward. This is my script for getting correlation id
var reqbody = request.body.dataString;
var parser = new global.JSON();
var parsedData = parser.decode(reqbody);
var correlationId = parsedData['CorrelationId'];
var detailsArray = parsedData['attach_details'];
for (i=0; i < detailsArray.length; i++) {
      gs.info("Type: " + detailsArray[i].Type
      + "\nName: " + detailsArray[i].Name);
}

 

Devi
Tera Contributor
var data = '{"@odata.context":"https://telisttest.crm.dynamics.com/api/data/v9.2/$metadata#incidents(ticketnumber,Incident_Annotati...))","value":[{"@odata.etag":"W/\"153965662\"","ticketnumber":"CAS-30320-T5T0L2","incidentid":"cdf04fe5-06b1-ec11-bea1-0003ff57a1ac","Incident_Annotation":[{"@odata.etag":"W/\"99811249\"","annotationid":"8e83bdf7-06b1-ec11-9841-000d3a5a3e97","objecttypecode":"incident","subject":"Note created on 3/31/2022 3:26:43 PM by SHUHEI ATSUMI [contact:f5e45587-a543-ea11-a812-000d3a5a17e3]","_objectid_value":"cdf04fe5-06b1-ec11-bea1-0003ff57a1ac"},{"@odata.etag":"W/\"152940868\"","annotationid":"d94b0b98-e594-8288-b280-29535933841e","objecttypecode":"incident","subject":"testing 15-04 ","_objectid_value":"cdf04fe5-06b1-ec11-bea1-0003ff57a1ac"},{"@odata.etag":"W/\"153837746\"","annotationid":"b0a730a5-35f5-03bb-804a-759991f8aeae","objecttypecode":"incident","subject":"Snow calling 28-04","_objectid_value":"cdf04fe5-06b1-ec11-bea1-0003ff57a1ac"},{"@odata.etag":"W/\"153837763\"","annotationid":"ba403a43-efee-1d4d-ab6b-a0c938726b2f","objecttypecode":"incident","subject":"snow 2","_objectid_value":"cdf04fe5-06b1-ec11-bea1-0003ff57a1ac"},{"@odata.etag":"W/\"153495211\"","annotationid":"28f9ed16-9e0b-54e4-a5d0-ddb16cd87436","objecttypecode":"incident","subject":"test 24-04 tetsed ","_objectid_value":"cdf04fe5-06b1-ec11-bea1-0003ff57a1ac"}],"Incident_Annotation@odata.nextLink":"https://telisttest.crm.dynamics.com/api/data/v9.2/incidents(cdf04fe5-06b1-ec11-bea1-0003ff57a1ac)/In..."},{"@odata.etag":"W/\"98834104\"","ticketnumber":"CAS-30318-J4J3N9","incidentid":"8ce79685-be99-ec11-b820-0003ff57b333","Incident_Annotation":[],"IncidentAnnotation@odata.nextLink":"https://telisttest.crm.dynamics.com/api/data/v9.2/incidents(8ce79685-be99-ec11-b820-0003ff57b333)/In..."}]}';


var str = JSON.stringify(data);

var parser = new global.JSON();

var parsedData = parser.decode(str);


var detailsArray = parsedData['Incident_Annotation'];

gs.print("detailsArraydetailsArray"+detailsArray);
 
I am getting detailsArraydetailsArray as undefined. Can you please help to retrieve the data for subject under Incident_Annoataion array