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.

In syntax, which is used to refer to that variable. Spaces are not allowed in variable names. It’s worth noting that changing a variable’s name has no effect on the data; all values linked with the variable remain unchanged. Renaming a variable merely changes its name while leaving the rest of its contents unchanged The Best Crossfit Gloves – Buying Guide and Reviews(2021 update) - Rigor Fitness: Fat Loss, Muscle Gain, Strength Gain.