Extract routeID from the postman response body

Dear All,
Roundtrip(chennai-bangalore,bangalore-chennai) bus journey for a passenger PNR defined as below example

{
“data”: [
{
“id”: “10DF123”,
“PNR”: “BD1U5M”,
“traveler”: {
“name”: {
“name”: “ABC”,
“surName”: “BCA”,
“prefix”: “MR”
},
“gender”: “M”
}
}
],
“includes”: {
“routeInfo”: {
“10FD0123A1”: {
** “id”: “10FD0123A1”,**
“isPrime”: false,
“busRoute”: {
“departure”: {
“toStation”: “CHENNAI”,
“at”: “2024-04-29T23:35:00”
},
“arrival”: {
“fromStation”: “BANGALORE”
},
“class”: “Sleeper”
}
},
“10FD0123A2”: {
** “id”: “10FD0123A2”,**
“isPrime”: false,
“busRoute”: {
“departure”: {
“toStation”: “BANGALORE”,
“at”: “2024-04-30T23:35:00”
},
“arrival”: {
“fromStation”: “CHENNAI”
},
“class”: “Sleeper”
}
}
}
}
}

here am facing trouble to extract id value from “includes.routeInfo.10FD0123A1[0].id” =10FD0123A1
“includes.routeInfo.10FD0123A2[0].id”= 10FD0123A2

from the above structure route1=10FD0123A1, route2=10FD0123A2 and so on if it is multiple trip of round trip (chennai-bangalore-goa)

Note: route(1…n) name is dynamic string and same copied to id value

Could you please help me to extract routeId value …!
Thanks in Advance

Please use the preformatted text option in the forum editor when pasting code or JSON responses. It stops everything aligning to the left.

Two things to note.

  1. Under the includes key, you have a series of unique objects. It’s not an array.
  2. The route starts with a number, so you will have to use bracket notation to target that element. You can’t use dot notation.

The following will loop through an array of routes, with a dynamic test to customize the test case name, so its easy to see which one has failed.

const response = pm.response.json();

let routes = ["10FD0123A1", "10FD0123A2", "10XXXXXXXX"]

routes.forEach(route => 
    pm.test(`${route} exists and id matches`, () => {
        pm.expect(route in response.includes.routeInfo).to.be.true;
        pm.expect(response.includes.routeInfo[route].id).to.eql(route);
    })
);

image

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.