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 :-

{
    "firstname": "Eric",
    "lastname": "Smith",
    "totalprice": 843,
    "depositpaid": false,
    "bookingdates": {
        "checkin": "2018-09-25",
        "checkout": "2019-03-13"
    }
} 
 

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:-

 
{
    "firstname": "Eric",
    "lastname": "Smith",
    "totalprice": 843,
    "depositpaid": false,
    "bookingdates": {
        "checkin": "2018-09-25",
        "checkout": "2019-03-13"
    }
} 
 

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 print all keys
var allKeys = Object.keys(responseInJson);
console.log("Keys are: ")
allKeys.forEach(function(key)
{
    console.log(key);
});

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

// To print all values
var allValues = Object.values(responseInJson);
console.log("Values are: ")
allValues.forEach(function(value)
{
    console.log(value);
});

To get key-value pair one by one:-

// To get key-value pairs
for (var key in responseInJson) {
  console.log("Key is "+ key +" and its value is :"+responseInJson[key])
   
}

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:-

// Assertion
pm.test("Assert values", function () {
    pm.expect(responseInJson.firstname).to.equal("Mark")
    pm.expect(responseInJson.firstname).to.not.equal("Sussan")
    pm.expect(responseInJson.firstname).to.eql("Mark");
    pm.expect(responseInJson.totalprice).to.be.above(100);
    pm.expect(responseInJson.totalprice).to.be.below(5000);
});

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

2 thoughts on “Postman Tutorial Part 34 – Extracting Value From JSON Object Response in Postman – JSON Object Parsing in Postman

Leave a Reply

Your email address will not be published. Required fields are marked *