How to extract values from JSON response in Postman?

3k Views Asked by At

I have a JSON Response :

[{
    "dateTime": 1603650600000,
    "supplierName": "Mr Orange GP ",
    "gender": "male",
    "supplierId": "788",
    "appointmentId": 454687,
    "tentativeAppointmentCode": "5f8ff4a8a9d5f"
}, {
    "dateTime": 1603650600000,
    "supplierName": "Mr Kennedy test ",
    "gender": "male",
    "supplierId": "600",
    "appointmentId": 573993,
    "tentativeAppointmentCode": "5f8ff4a8a9d5f"
}, {
    "dateTime": 1603651500000,
    "supplierName": "Mr Orange GP ",
    "gender": "male",
    "supplierId": "788",
    "appointmentId": 454688,
    "tentativeAppointmentCode": "5f8ff4a8a9d5f"
}, {
    "dateTime": 1603651500000,
    "supplierName": "Mr Kennedy test ",
    "gender": "male",
    "supplierId": "600",
    "appointmentId": 573994,
    "tentativeAppointmentCode": "5f8ff4a8a9d5f"
}]

I need to extract the values of first occurrence of dateTime, AppointmentId and tentativeAppointmentCode. Tried below mentioned code but didn't work in this case.

var jsonData=JSON.parse(responseBody);
postman.setEnvironmentVariable("dateTime",jsonData.dateTime);

How can I extract these values and store in a variable so that I can use it in other requests?

2

There are 2 best solutions below

0
On

I have done something like this for storing a token for later use

const jsonData = pm.response.json();
pm.environment.set("token", jsonData.access_token);
2
On

Assuming, by "reservation ID" you mean tentativeAppointmentCode (reservation ID doesn't occur in your response body), the following will do it:

let jsonData = pm.response.json();

pm.environment.set("dateTime",jsonData[0].dateTime);
pm.environment.set("appointmentId",jsonData[0].appointmentId);
pm.environment.set("tentativeAppointmentCode",jsonData[0].tentativeAppointmentCode);