Json Data Extraction in Postman

Postman is a test tool to makes API development faster, easier, and better for development teams. It lets you automate your API tests and get early feedback in the development stage. Data extraction in postman article will cover two feature:

  • Data extraction from a web service
  • How to use this data in another web service call

How to Extract Data from JSON response in Postman?

All Postman request has a section called “Tests”. This is where you do all scripting for data extraction. This is the basic command to extract response body into a variable.

var jsonData = JSON.parse(responseBody);

Now your response body is stored in a variable called jsonData.

Now you are able to fetch any data from the response by using JsonPath expressions.

Our response is below:

{
    "data": {
        "authToken": "b0d71c7f-6b6d-4819-9236-4320cc71c341",
        "refreshToken": "4818e0dd-e04e-44c4-8d59-1e369a753746",
        "expirationDate": 1551862078500,
        "userUniqueId": "53f13d594327d166a891ccad57c42d2d"
    },
    "status": "success"
}

So we extract the authentication token by using below expression.

pm.globals.set("access_token",jsonData.data.authToken);

Now, authentication string is stored in a global environment variable. This variable’s name is “access_token” for this example.

data extraction in postman

How to Use Extracted Data?

We want to use globally set value in another authenticated web service call. Let’s do it.

All you need to do is use {{variable_name}} convention to use the extracted value in a Potman request. You can use it in a URL, Header or file body. All you need to do is using the same convention.

Now you can your run tests and see that your second web service will use the authentication token fetched from the first service.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.