Virtual Agent - Parse JSON response headers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Monday
Hi,
I have a requirement where I need to display the response headers from the JSON payload. I am implementing it in virtual agent. Scenario is:
1. Flow designer makes outbound rest call and stores the entire response body in a script variable 'response_body' in virtual agent.
response_body:
{
"queryId":"#snow_va_user_info",
"executedQuery":"session.logins | where user.ad.email_address == \"abc@abc.com\" | list device.name, user.ad.email_address , device.last_seen , device.entity , device.collector.uid , device.hardware.model , device.boot.days_since_last_full_boot , user.last_seen",
"rows":5,
"executionDateTime":{"year":2026,"month":4,"day":21,"hour":22,"minute":9,"second":33},
"headers":["device.name","user.ad.email_address","device.last_seen","device.entity","device.collector.uid","device.hardware.model","device.boot.days_since_last_full_boot","user.last_seen"],
"data":[
["USRRGBS","abc@abc.com","2026-04-21 18:34:17","USA-MO-Remote","7648adc4-2636-4e81-b366-00ac722c5","HP EliteBook 840 14 inch G11 Notebook PC",1,"2026-04-21 17:56:25"],
["USRRGBS","abc@abc.com","2026-04-21 18:34:17","USA-MO-Remote","7648adc4-2636-4e81-b366-00ac78202","HP EliteBook 840 14 inch G11 Notebook PC",1,"2026-04-21 17:56:25"],
["USRRGBS","abc@abc.com","2026-04-21 18:34:17","USA-MO-Remote","7648adc4-2636-4e81-b366-0082022c5","HP EliteBook 840 14 inch G11 Notebook PC",1,"2026-04-21 17:56:25"]
]
}
2. There is a script action step which will parse this data and extract the header part from the above string.
Script I am using:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Monday
hey @Saumya Awasthi1
try this :
(function execute() {
try {
// Step 1: Get raw response
var raw = vaVars.response_body;
gs.info('VA Debug | Raw response type: ' + typeof raw);
gs.info('VA Debug | Raw response value: ' + raw);
// Step 2: Normalize to object (handle both STRING and OBJECT cases)
if (!raw) {
gs.error('VA Error | response_body is empty or null');
return;
}
var parsedData;
if (typeof raw === 'string') {
parsedData = JSON.parse(raw);
} else if (typeof raw === 'object') {
parsedData = raw;
} else {
gs.error('VA Error | Unsupported response_body type');
return;
}
// Step 3: Validate structure
if (!parsedData.headers || !parsedData.data) {
gs.error('VA Error | Invalid response structure: headers or data missing');
gs.info('VA Debug | Parsed object: ' + JSON.stringify(parsedData));
return;
}
// Step 4: Extract headers
var headers = parsedData.headers;
gs.info('VA Debug | Headers: ' + JSON.stringify(headers));
// Step 5: Get index positions
var deviceNameIndex = headers.indexOf('device.name');
var deviceUidIndex = headers.indexOf('device.collector.uid');
gs.info('VA Debug | device.name index: ' + deviceNameIndex);
gs.info('VA Debug | device.collector.uid index: ' + deviceUidIndex);
if (deviceNameIndex === -1 || deviceUidIndex === -1) {
gs.error('VA Error | Required headers not found');
return;
}
// Step 6: Extract data using header index
var data = parsedData.data;
var result = [];
for (var i = 0; i < data.length; i++) {
var row = data[i];
result.push({
device_name: row[deviceNameIndex],
device_uid: row[deviceUidIndex]
});
}
// Step 7: Log final result
gs.info('VA Debug | Final extracted result: ' + JSON.stringify(result));
// Step 8: Store result back to VA variable (optional)
vaVars.extracted_devices = result;
} catch (ex) {
gs.error('VA Exception | ' + ex.message);
}
})();*************************************************************************************************************************************
If this response helps, please mark it as Accept as Solution and Helpful.
Doing so helps others in the community and encourages me to keep contributing.
Regards
Vaishali Singh
