Postman-Extract value from the JSON object array
By: Knoldus Inc.
Tags: Json, Extractdata, Jsonobject Aaray, Postman

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.

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 :