Postman-Extract value from the JSON object array

Reading Time: 3 minutes

Most of us are using postman for automating the Rest API. In automating the requests, we need to pass the data in many requests in different forms. It will be in the datasheet/excel form, database and also sometimes need to extract data from JSON response of any request. Postman lets you write scripts that run before/after you receive a response from the server. The pre-request and test scripts run inside a Postman help to do that. So that you can use it to extract the data from the JSON array.

So many of us facing problems to extract the data from the long nested JSON in postman.

Here is the solution for that, please follow these steps as per your requirements.

  • Now extract the data from the response of the nested JSON  object array schema. I also need to use it in the next chaining request.

JSON object array example:-

{
	"componentName": "Devices",
	"componentKey": "devices",
	"columns": [{
			"columnName": "Device Id1",
			"columnKey": "deviceId2",
			"show": true
		},
		{
			"columnName": "Device Id2",
			"columnKey": "deviceId2",
			"show": true
		},
		{
			"columnName": "Device Id3",
			"columnKey": "deviceId3",
			"show": true
		},
		{
			"columnName": "Device Id4",
			"columnKey": "deviceId4",
			"show": true
		}
	]
}

 Now we have to extract all the value of columnKey in the same format which is given in the below image. I know it is very simple if we extract this from any online tool like JsonExtractor with Json Syntax- $.columns[*].columnKey, given in the below image

  •  In Postman we cannot use the [*] to get all value of any object. In place of [*], here we will use for a loop. So let’s move how we will extract it using Postman.
  1. Firstly define a variable with parsing the JSON response body and store it in a defined variable.

responseJson = JSON.parse(responseBody);

2. Now find the length of the column so that we are able to know the occurrence of the column key and also define the empty array.

var a=[];
var list = (responseJson.columns).length;

3. Next, we will use the for loop to iterate the columns till its length and extract the column key at each position. Then append the elements in that array using push.

 for (var i = 0; i < list; i++) 
                  {
                   var counter = responseJson.columns[i];
                    schID=counter.columnKey 
                     a.push(schID)
                   }

4. Now use the JSON Stringify to convert into the JSON string and store the output in the environmental variable of the postman-

 a= JSON.stringify(a)
   postman.setEnvironmentVariable("schID", a);

Here is full code to extract the data –

  responseJson = JSON.parse(responseBody);
  var a=[];
var list = (responseJson.columns).length;
console.log(list);
   for (var i = 0; i < list; i++) 
    {
    var counter = responseJson.columns[i];
    schID=counter.columnKey

    a.push(schID)
    }
    a= JSON.stringify(a)
    postman.setEnvironmentVariable("schID", a);

So according to your JSON response, you can edit it to extract the data. It is just an example to solve the problem which generally we are facing while using postman. It will definitely work and if you will face such type of any problem or error in postman, please communicate with the help of the comment on this blog and let me know pretty simple! Isn’t it?.

References :

https://learning.postman.com/docs/postman/scripts/test-examples/

https://community.postman.com/t/json-stringify-request-body-using-raw/5526

Written by 

I have around 5 Year of Experience in Software Testing. I am working as API Tester and Performance Testing and Used the tools like Jmeter,Postman, ReadyAPI(SoapUI),LoadUI .I have Knowledge of core concepts of manual and automation both and also have knowledge in basic of Selenium Webdriver.I always ready to adopt any environment and zeal to learn the new tools & technologies

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading