Postman Tutorial Part 34 – Extracting Value From JSON Object Response in Postman – JSON Object Parsing in Postman

As we know well now that Postman helps you to create automated tests for API testing, we need to assert response with expected values. JSON and XML are widely used type for request and response to API. In this post, we will see parsing JSON response and extracting values. Mainly we will see parsing a JSON object in this post.

What is JSON?

As per official website of JSON :-

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

Parsing JSON in Postman:

When we hit a GET request ( https://restful-booker.herokuapp.com/booking/1 ) , it gives below JSON response :-

Now for assertion, we need to extract values of different keys and assert with expected values. Above response represents a JSON Object enclosed in {}. “firstname” , “lastname” etc. are members of main JSON Object. “bookingdates” is another named JSON Object. “checkin” and “checkout” are two members of “bookingdates ” JSON Object.

Before extracting values from JSON response, we need to parse it first. We can parse response into JSON using below statement:-

var responseInJson = pm.response.json();

You may write below statement as well but it is deprecated:-

var responseInJson = JSON.parse(responseBody)

Once you parse response in to JSON, you can consider response as below:-

Now you just need to traverse (Similar to Json Path concept) to desired key to extract value.

To get value of firstname : responseInJson .firstname

To get value of totalprice: responseInJson .totalprice

To extract value of checkin : responseInJson.bookingdates.checkin

Example:-

Postman Console:-

To get an array of all keys of a JSON Object:-

Note:- You will not get nested keys like checkin, checkout. To get those keys you need to pass ” bookingdates” as JSON Object.

To get an array of all values of a JSON Object:-

To get key-value pair one by one:-

Note:- I used responseInJson[key] instead of responseInJson.key. We need to use dot operator when it is constant and square bracket when key is not constant so that it will evaluate the value of key first before searching for the value in JSON.

Applying some assertions on values:-

To assert actual value is matching to expected value:

pm.expect(responseInJson.firstname).to.equal(“Mark”);

pm.expect(responseInJson.firstname).to.eql(“Mark”);

To assert actual value is not equal to some value:-

pm.expect(responseInJson.firstname).to.not.equal(“Sussan”)

To assert price is greater than a value:-

pm.expect(responseInJson.totalprice).to.be.above(100);

To assert price is less than a value:-

pm.expect(responseInJson.totalprice).to.be.below(5000);

Script example:-

Import above example collection from below link:-

https://www.getpostman.com/collections/ce87924629b29cf01cbe

Refer basics of API Testing here.

Refer detailed tutorial of Postman here.

More about API Testing in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyApiTesting

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.
%d bloggers like this: