- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-26-2023 11:15 AM
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'];
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-26-2023 12:24 PM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-26-2023 12:24 PM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-30-2025 06:17 AM