How to get a value from variable when response in under id

I want add Id number to Environment variable
I used

const TestCaseInfo = pm.response.json()
pm.environment.set(“TestCaseID” , TestCaseInfo.id);

not able to get " id" due to Id is under the “12871” ,and 12871 changing every run .

here is the response
{

"12871": {

    "id": 12871,

    "orderId": 11415,

    "executionStatus": "-1",

    "executionWorkflowStatus": "CREATED",

    "comment": "",

    "htmlComment": ""

}

Hi @skondur, Welcome to the community.

The JSON provided by you seems to be invalid. Taking below valid JSON as an example.

{
"12871": {
    "id": 12871,
    "orderId": 11415,
    "executionStatus": "-1",
    "executionWorkflowStatus": "CREATED",
    "comment": "",
    "htmlComment": ""
}
}

It seems the value of id is coming in key of JSON object. One way to extract the object is to extract keys from the JSON object.

const mark1= pm.response.json();
const key = Object.keys(mark1)[0];//assuming, the first key is your ID
console.log(key);

Hope this helps.

1 Like

Hi Pranav ,
yes I missed } at the end , but Solution you provided worked . thanks for the Quick response.

Thanks
Kondur.