Use IF statement to check if data field in nested array exists, and if it exists set the value to a variable

My question: I want to capture data from three objects in a JSON response, however, two of the objects are always in the response, and one is not always there (by design). Additionally, the first two objects that are always there (notificationId & receiptHandle), are inside an array, and the 3rd object (actionUrl) is nested inside a second array (if it exists).

When I try to set these three variables using my normal method, shown below, postman errors out when it cannot find the actionUrl and hence none of the variables are set (even though the other two are present)

let jsonData = pm.response.json();
let notificationId = jsonData.notifications[0].notification.id;
pm.environment.set("notification_id", notificationId);
let receiptHandle = jsonData.notifications[0].receiptHandle;
pm.environment.set("receipt_handle", receiptHandle);
let terminalActionUrl = jsonData.notifications[0].notification.message.data.actions[0].href;
pm.environment.set("action_url", actionUrl);

When I submit the above I get:

TypeError: Cannot read properties of undefined (reading '0')

When the data for all 3 objects is in the response, everything gets saved correctly. And if I remove the 3rd variable it saves the first two fine.

So, I am hoping someone can help me with writing an IF statement for the third variable (the actionsUrl) so that it only tries to set it if it’s found (in the nested array) but I cannot seem to get it to work (after trying many many variations and searching on this and other forums…)

Here’s an example of what the JSON response looks like with just the first two data points available:

{
    "notifications": [
        {
            "receiptHandle": "{some hash code}",
            "notification": {
                "id": "{some GUID}",
                "message": {
                    "id": "{some GUID}",
                    "data": {
                        "some": "data"
                    }
                }
            }
        }
    ]
}

And here’s an example of it with all three:

{
    "notifications": [
        {
            "receiptHandle": "{some hash code}",
            "notification": {
                "id": "{some GUID}",
                "message": {
                    "id": "{some GUID}",
                    "data": {
                        "some": "data",
                        "actions": [
                            {
                                "href": "{some URL}"
                            }
                        ]
                    }
                }
            }
        }
    ]
}

Here is a quick and dirty way of doing it using typeOf (which will prevent the runtime error you are receiving).

const response = pm.response.json().data

let notificationId = response.notifications[0].notification.id;
console.log(notificationId);

let receiptHandle = response.notifications[0].receiptHandle;
console.log(receiptHandle);

if (typeof response.notifications[0].notification.message.data.actions == "object") {
   let terminalActionUrl = response.notifications[0].notification.message.data.actions[0].href
   console.log(terminalActionUrl);
}

You can put another IF statement in there if you want to dig deeper into the array.

Checking for the non-existence of something is not that easy.

Have a look at the following topic for a more in-depth conversation on the subject.

debugging - Check if object exists in JavaScript - Stack Overflow

Thanks @michaelderekjones for the input… unfortunately I wasn’t able to get that working even with multiple altered attempts… not sure what I was doing wrong.

In the end though I was able to get this working using the below code. Essentially it searches the array for the object and if it’s defined it performs the action (set the variable), and if not just logs a “could not find” message.
(note: I ended up using the IF statement logic for the first two variables given there is a slight chance the notifications array could be empty so figured I may as well add them in to be safe)

Additionally, the third variable uses a nested IF statement because it’s an array inside an array.

Hopefully this helps someone else in the future!

let jsonData = pm.response.json();

if ((jsonData.notifications.find(c => c.receiptHandle)) != undefined) {
    let receiptHandle = jsonData.notifications[0].receiptHandle;
    pm.environment.set("receipt_handle", receiptHandle);
    console.log("Resolved receiptHandle: " + pm.environment.replaceIn('{{receipt_handle}}'));
}
else {
    console.log("Couldn't find receipt handle");
}

if ((jsonData.notifications.find(c => c.notification.id)) != undefined) {
    let notificationId = jsonData.notifications[0].notification.id;
    pm.environment.set("notification_id", notificationId);
    console.log("Resolved notification.id: " + pm.environment.replaceIn('{{notification_id}}'));
}
else {
    console.log("Couldn't find notification ID");
}

if ((jsonData.notifications.find(c => c.actions)) != undefined) {
    if ((jsonData.notifications[0].notification.message.actions.find(c => c.href)) != undefined) {
        let terminalActionUrl = jsonData.notifications[0].notification.message.data.actions[0].href;
        pm.environment.set("terminal_action_url", terminalActionUrl);
        console.log("Resolved action link: " + pm.environment.replaceIn('{{terminal_action_url}}'));
    }
}
else {
    console.log("Couldn't find actions link");
}
1 Like