Open In App

How to read Array of Nested JSON Response in Postman ?

Last Updated : 27 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We know that the postman is the one who delivers the letters sent by your friends, colleagues, or relatives and acts as a bridge of communication. Here Postman refers to the development of API that acts as a bridge or communication between different programs.

In other words, it is an API client, made for developing, testing, and management. You can download Postman on Windows, Linux, and Mac.

JSON stands for JavaScript Object Notation where data is in the form of key-value pairs. It is an alternative way to send or transmit data between servers and web applications. It starts and ends with curly brackets {}.

Sample JSON Data:

We have here an object that contains the property’s name, age, city, hobbies, etc. The key is name and the value here in this example is Jhonson.

{
"name": "Johnson",
"age": 42,
"city": "New Castle",
"hobbies": ["boxing", "traveling", "reading"],
"Roll No ": 123,
}

Nested JSON Example:

Nested JSON object is defined as the JSON Object included in another object like in this example. We have included object address which have its own object street, city and zip code. Complex JSON includes combination of objects and arrays.

{
"name": "Johnson",
"age": 42,
"address": {
"street": "123 Street",
"city": "New Castle",
"zipcode": "NC1210003"
},
"Roll No": 123,
}

How Postman works?

  • As you know we in the Postman send request to the server and get in return response from the server back.
  • Open the desktop app and using + sign create a new workspace. Enter the url required and use get to fetch the data.
  • It is not required to use https://. Postman Get method itself add it.
  • GET method is to get the data from the url.
  • POST method is to create the new data.
  • PUT method is to update the data.
  • PATCH method is to partially update the data.
  • DELETE method is to delete the data.
  • If we search HTTPS method, we will get various API method.
  • For every request there is a response whether it has any contents or not present.

Let’s go to the website https://reqres.in/.

Presentation156

url

  • Now copy the request /api/users/2 and type on the website https://reqres.in/api/users/2
p119

users2

  • Now go to the postman and type on Get method the complete url https://reqres.in/api/users/2.
p129

postman

  • You can add all these get data into your own collection and name it as per your wish. When you click on the plus (+) sign, it creates new collection. Let’s name it as collection 1. We can add request and transfer the request created earlier, here we created Get Data1.
p798

Get Data1

  • We can create new folder and name it as GET.
p30

GET

  • Now you can just from the three dots select run collection and it will run.
p54

Run

  • We can add variables by clicking on edit on collection 1 and adding the key, value.
p76

variables

  • Now got to the Get data 1 and instead of website https://reqres.in/ type {{url}}.
p55

Variables

  • We can then run send to check.
p58

Variable

  • We can add in the test to get the response.
console.log("Hello World....");
let m = pm.variables.get("url");
console.log("the url for variable is:"+m);

  • Now open the Postman console by Ctrl+Alt+C. Save and then run the test.
p49

Postman Console

Reading JSON

We will write some test to check the status and get response as JavaScript object.

pm.test("Status code is 200", function(){
pm.response.to.have.status(200);
});
pm.test("Validation of JSON", function(){
const res= pm.response.json();
console.log(res);
});
  • Now when we save and run it, we get all the JavaScript object.
p74

JSON Object

  • As you can see that we have data and it has id equal to 2. To validate we will write in test and run it.
pm.test("Status code is 200", function(){
pm.response.to.have.status(200);
});
pm.test("Validation of JSON", function(){
const res= pm.response.json();
pm.except(res.data[0].id).to.eql(2);
});
o77

validation of id

  • If I write 7 instead of 2, my test gets failed.
o99

Failed validation



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads