Parse JSON Response + Log a Specific Field from the Response

Parse JSON Response Log a Specific Field from the Response

Today, we will explain the following piece of Postman script that contains one of the most commonly used features in testing: console.log

const response = pm.response.json();

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

console.log(response.booking.firstname);

Let’s break this script down for better comprehension

1. Parse JSON Response:

const response = pm.response.json();
  • This line converts the raw response from the API into a JavaScript object using the pm.response.json() method.
  • The response variable now holds the entire JSON structure returned by the API, which you can access like any other JavaScript object.

2. Status Code Test:

pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
})
  • This is a Postman test to ensure that the API responds with a 200 status code, which typically indicates a successful request.
  • If the status code returned by the server is 200, the test will pass. Otherwise, the test will fail.

The Status Code Test isn’t a must-have in this case, but you may want to ensure the response is valid in some other way. If you’re relying on console.log(), it might show undefined or other errors if the status code is not 200, but you won’t have a clear failure indication in Postman. That’s why it is good to have it here.

3. Log a Specific Field from the Response:

console.log(response.booking.firstname);
  • This line logs the value of firstname inside the booking object to the Postman console.
  • The response.booking.firstname is accessing the firstname field from the booking object, which was parsed from the response body.
  • The output will be visible in the Postman console (accessible via View > Show Postman Console) after you send the request.

Now let’s look at an example of a JSON body text that we are referring to with the script above:

{
    "bookingid": 2917,
    "booking": {
        "firstname": "Peter",
        "lastname": "The Great",
        "totalprice": 153,
        "depositpaid": true,
        "bookingdates": {
            "checkin": "2024-09-20",
            "checkout": "2024-09-23"
        },
        "additionalneeds": "Breakfast, Lunch, Dinner"
    }
}

The response from the console.log command refers to this whole JSON body text.
The booking refers to the folder where we will find our requested item.
Finally, the firstname refers to the item (or value) we seek.

The console.log will print the value of the requested item in the console just like this:

This method can be useful for the following purposes:

  • Identify any issues with your script, such as incorrect field names or unexpected response formats.
  • When an API response returns dynamic data (like bookingid or timestamp), logging it helps ensure that you understand the data being returned.
  • You can also validate that the API returns the expected fields and values.
Scroll to Top