Can not create a variable from a request body

I have the below Jason body from a request. I am trying to make variable or capture the total information (In this case 1). This is the error that im getting. “directly is not defined”

-----------------------Script Used---------------------------------------

const response = pm.response.json();

const total = response.used-directly.map(entry => entry.total);

console.log(names);
-----------------------Script Used---------------------------------------

-----------------------JSON BODY---------------------------------------

{
“used-directly”: {
“total”: 1,
“objects”: [
{
“uid”: “a974688c-632d-428d-aa7f-1eee2e6b13db”,
“name”: “Sentinel SIEM POC - AzureARC”,
“type”: “group”,
“domain”: {
“uid”: “41e821a0-3720-11e3-aa6e-0800200c9fde”,
“name”: “SMC User”,
“domain-type”: “domain”
},
“icon”: “General/group”,
“color”: “black”
}
],
“threat-prevention-rules”: ,
“nat-rules”: ,
“access-control-rules”: ,
“https-rules”:
}
}
-----------------------JSON BODY---------------------------------------

The issue is with the hyphen which breaks the dot notation, so you will need to use bracket notation to target this element.

However, the value for the used-directory key is an object, not an array.

The JavaScript map function only works against arrays.

If you want to console log the total, then the following should work against your example response.

const response = pm.response.json();
const total = response["used-directly"].total 
console.log(total); // 1

Can you please use the preformatted text option in the editor when pasting code or example JSON. It stops everything being treated as a paragraph and aligning to the left.

{
    "used-directly": {
        "total": 1,
        "objects": [
            {
                "uid": "a974688c-632d-428d-aa7f-1eee2e6b13db",
                "name": "Sentinel SIEM POC - AzureARC",
                "type": "group",
                "domain": {
                    "uid": "41e821a0-3720-11e3-aa6e-0800200c9fde",
                    "name": "SMC User",
                    "domain-type": "domain"
                },
                "icon": "General/group",
                "color": "black"
            }
        ],
        "threat-prevention-rules": [],
        "nat-rules": [],
        "access-control-rules": [],
        "https-rules": []
    }
}

That worked. Thank you very much for your help

I will used the Preformatted option next time.

1 Like