Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Virtual Agent - Parse JSON response headers

Saumya Awasthi1
Tera Contributor

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:

(function execute() {
var parsedData = JSON.parse(JSON.stringify(vaVars.response_body));
var headers = parsedData.headers;
gs.info('headers are:' + headers);
var deviceNameIndex = headers.indexOf('device.name');
var deviceUidIndex = headers.indexOf('device.collector.uid');
gs.info('name index is ' + deviceNameIndex);
gs.info('uid index is ' + deviceUidIndex);
})()
 
Question: When I run this script, I am getting all 3 values as undefined under all 3 gs.info messages above. Can someone please suggest what am I doing wrong?
Thank you in advance!
1 REPLY 1

vaishali231
Tera Guru

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