Search code examples
jsonpostman

How to extract value from json object response in postman


I got this response :

"success": true,
    "data": {
        "638f2a029a8253170d1272c7": {
            ...
            "id": "638f2a029a8253170d1272c7",    // this is the value i want
            "permissions": {
                ...
            }
        },
        ...
    }
}

the value I want is the id: "id": "638f2a029a8253170d1272c7"

I tried this:

let responseData = pm.response.json();
pm.environment.set("customerid", responseData.data[0].id);`

but getting error


Solution

  • Correct code would be:

    let responseData = pm.response.json(); 
    
    //get the first key of `data` object --> the `id` you want
    pm.environment.set("customerid", Object.keys(responseData.data)[0]);
    //638f2a029a8253170d1272c7