Johan Ahl
Johan Ahl

Reputation: 43

Postman - Failing to get value from an object that has `:` in the property key

I'm struggling to get value from an object that has : within the key name of property.

This is how my Response Body looks:

{
    "links": {
        "content": {
            "href": "http://*********",
            "templated": false,
            "type": "application/hal+json"
        },
        "test:search": [
            {
                "title": "Some title",
                "href": "http://*************",
                "type": "application/hal+json"
            }
        ]
    }
}

When I try to get the href value from test:search, Postman is giving me an error:

"Missing ";" before statement".

Upvotes: 4

Views: 410

Answers (1)

Danny Dainton
Danny Dainton

Reputation: 25881

This should work if you add it to the Tests tab:

_.each(pm.response.json().links['test:search'], (item) => {
    console.log(item.href)
    pm.environment.set('href', item.href)
})

Test Request

If you were to use this to reference the property pm.response.json().links.test:search it would fail to set the variable.

Failed Request

Upvotes: 1

Related Questions