skip to Main Content

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

2

Answers


  1. you can get the id field value from JSON in this way.

    JSONObject jsonObj = new JSONObject(responseJson);
    System.out.println(jsonObj.getString("id));

    Login or Signup to reply.
  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search