- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2025 10:55 PM - edited 05-15-2025 11:00 PM
I am facing issue parsing certain attributed in below response body from an API. I am writing in script include and getting parsing error.
What I want - For every 'requestkey' in requestParams, there can be 1 or multiple "Tasks". So want to get the "Task ID" and state for every request id. If there are 10 tasks under requestkey, the logic must output 10 task id and their respective states. Refer below response sample.
{
"requestDetails": {
"comments": "mycomments",
"requestParams": [
{
"requestkey": "342",
"Start Date": "2025-05-15 12:44:25",
"Request Type": "AddRequest",
"value": "asasasasa01G3PCBKB170TEP3CC2BBH",
"status": "Request Created"
}
],
"Tasks": [
{
"Task ID": 999,
"State": "New",
"comments": " test comments"
} // these tasks could be multiple too each with a diff ID and State
],
},
"msg": "SUCCESS",
"errorCode": "0"
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2025 11:27 PM
Hi @Snehal13 ,
how to find which tasks inside the task array are related to requestkeys as both are keys of same level and both are of array types requestParams and Tasks on same level?
please share the script that you are facing the issue with
if requestParams array is always going to be of length 1 you can try this
var TaskExtractor = Class.create();
TaskExtractor.prototype = {
initialize: function() {},
extractTasks: function(responseBody) {
var result = [];
try {
var parsed = JSON.parse(responseBody);
if (parsed.requestDetails) {
var requestParams = parsed.requestDetails.requestParams || [];
var tasks = parsed.requestDetails.Tasks || [];
for (var i = 0; i < requestParams.length; i++) {
var requestKey = requestParams[i].requestkey;
for (var j = 0; j < tasks.length; j++) {
var task = tasks[j];
result.push({
requestkey: requestKey,
task_id: task["Task ID"],
state: task["State"]
});
}
}
}
} catch (e) {
gs.error("JSON parsing error in TaskExtractor: " + e.message);
}
return result;
},
type: 'TaskExtractor'
};
test
var response = '{ "requestDetails": { "comments": "mycomments", "requestParams": [ { "requestkey": "342", "Start Date": "2025-05-15 12:44:25", "Request Type": "AddRequest", "value": "asasasasa01G3PCBKB170TEP3CC2BBH", "status": "Request Created" } ], "Tasks": [ { "Task ID": 999, "State": "New", "comments": " test comments" }, { "Task ID": 1000, "State": "In Progress", "comments": "another task" } ] }, "msg": "SUCCESS", "errorCode": "0" }';
var extractor = new TaskExtractor();
var taskList = extractor.extractTasks(response);
gs.info(JSON.stringify(taskList));
Result
[
{
"requestkey": "342",
"task_id": 999,
"state": "New"
},
{
"requestkey": "342",
"task_id": 1000,
"state": "In Progress"
}
]
FYI:Used copilot
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2025 11:27 PM
Hi @Snehal13 ,
how to find which tasks inside the task array are related to requestkeys as both are keys of same level and both are of array types requestParams and Tasks on same level?
please share the script that you are facing the issue with
if requestParams array is always going to be of length 1 you can try this
var TaskExtractor = Class.create();
TaskExtractor.prototype = {
initialize: function() {},
extractTasks: function(responseBody) {
var result = [];
try {
var parsed = JSON.parse(responseBody);
if (parsed.requestDetails) {
var requestParams = parsed.requestDetails.requestParams || [];
var tasks = parsed.requestDetails.Tasks || [];
for (var i = 0; i < requestParams.length; i++) {
var requestKey = requestParams[i].requestkey;
for (var j = 0; j < tasks.length; j++) {
var task = tasks[j];
result.push({
requestkey: requestKey,
task_id: task["Task ID"],
state: task["State"]
});
}
}
}
} catch (e) {
gs.error("JSON parsing error in TaskExtractor: " + e.message);
}
return result;
},
type: 'TaskExtractor'
};
test
var response = '{ "requestDetails": { "comments": "mycomments", "requestParams": [ { "requestkey": "342", "Start Date": "2025-05-15 12:44:25", "Request Type": "AddRequest", "value": "asasasasa01G3PCBKB170TEP3CC2BBH", "status": "Request Created" } ], "Tasks": [ { "Task ID": 999, "State": "New", "comments": " test comments" }, { "Task ID": 1000, "State": "In Progress", "comments": "another task" } ] }, "msg": "SUCCESS", "errorCode": "0" }';
var extractor = new TaskExtractor();
var taskList = extractor.extractTasks(response);
gs.info(JSON.stringify(taskList));
Result
[
{
"requestkey": "342",
"task_id": 999,
"state": "New"
},
{
"requestkey": "342",
"task_id": 1000,
"state": "In Progress"
}
]
FYI:Used copilot
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2025 11:33 PM
Yes. requestParams array is always going to be of length 1 but Tasks array will be of length 1 or more
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2025 11:37 PM
Cool
you can try the solution that I have shared
or share the script that you are having issues with
how do you want the solution to be(please share the output structure you want it to be in)
or check if The last section Result section in my response is okay
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya(
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2025 12:00 AM
Noted. Let me try this and come back here.