Extract value from a JSON Response Body

Hi there,

I have the below response body and would like to save the “url” value from the property to a collection Variable.

{
    "status": "success",
    "pageStatus": 200,
    "scrapeDate": "2022/11/21",
    "scrapeHour": "21:24:29",
    "results": {
        "scopedto": ".class",
        "links": [
            {
                "text": "",
                "host": "abc.com",
                "url": "https://abc.com/defg/",
                "radiusContext": {
                    "parentText": "akajsdfhs",
                    "nextSiblingText": "akajsdfhs"
                }
            }
        ],
        "partialPaths": [],
        "imageUrls": [
            {
                "host": "example.com",
                "url": "https://www.example.com/kasjdhksdhfkjsdhkjhsdfk"
            }
        ]
    }
}

I’ve already tried:
let response = pm.response.json();
pm.collectionVariables.set(“url”, response.results.links.url);
or
pm.collectionVariables.set(“url”, response.results[0].links[0].url);
and various variations of this.

I feel like I’m so close, but can’t figure it out.

I’ve googled around for things like “extract property JSON” and “select key value pair in JSON with javascript”, to no avail.

Any help is welcome. Thank you!

Looks like it should be response.results.links[0].url

The results property isn’t an array, it is an object so you wouldn’t need to specify an index.

1 Like

Ahhhh, I was so close :smiley:
Yep, that works, thank you very much!

1 Like