How to Test JSON Properties in Postman

How to Test JSON Properties in Postman

User Avatar

JSON (JavaScript Object Notation) is one of the most used formats for sending and receiving API responses. With the rise of REST APIs, the JSON format has started gaining popularity among technologists. XML had similar popularity and usage, but that popularity has dropped over time with the adoption of JSON among tech communities.

In this blog post, we’ll walk through an overview of JSON architecture, structure, and its examples. Then, we’ll review some code snippets that will help us access and test JSON properties using Postman.

JSON architecture

JSON is a text-based data format that’s used to represent data in a structured way based on JavaScript object syntax. As mentioned earlier, it’s the most commonly used format for data exchange between two different machines running at different locations, or between two applications running on the same machine.

Although it closely resembles JavaScript object syntax, it can be used independently with various programming languages. Due to its popularity and human-readable standards, most programming languages have started supporting the JSON format within their pre-built libraries, which helps users to create or parse JSON data.

JSON data can be easily stored in a file with a file extension called .json and MIME type as application/json. JSON supports the two most used data structures, arrays and objects. Since these data structures are foundational pillars of modern programming languages, JSON is a strong and useful data-exchange format.

JSON structure

As described above, JSON structure is generated using arrays and objects. This means almost all the basic data structures like strings, numbers, booleans, and other object literals are also supported in the JSON format, making it easier for users to read, manage, build, and test the data with their other software programs. Since JSON is human-readable, it allows users to generate simple to complex datasets with minimal effort.

For example, the simplest dataset can be an object starting with { and ending with } having a key/value pair where keys are name, email ID, Twitter, and GitHub handle, and values are as shown in the object below.

{
   "email": "[email protected]",
   "name": "JSON",
   "twitter": "@callmeJSON007",
   "github": "helloworldjsonhere"
}

And the complex dataset can be a combination of objects and arrays, as shown below. The complexity of the structure can be increased as needed, depending on the requirements and accessibility.

{
   "items": [
       {
           "orderID": "0000001211",
           "orderInvoiceNo": "1234567",
           "OrderBlocks": [
               {
                   "lineNo": [1,4,6,8,9,1,4],
                   "ProductCode": "#31451"
               },
               {
                   "lineNo": 2,
                   "ProductCode": "#64311"
               },
               {
                   "lineNo": 3,
                   "ProductCode": "#85959"
               }
           ]
       },
       {
           "orderID": "0000001212",
           "orderInvoiceNo": "1234568",
           "OrderBlocks": [
               {
                   "lineNo": 7,
                   "ProductCode": "#86869"
               },
               {
                   "lineNo": [6,7,4,8,4,2],
                   "ProductCode": "#10384"
               },
               {
                   "lineNo": 12,
                   "ProductCode": "#00873"
               }
           ]
       },
       {
           "orderID": "0000001213",
           "orderInvoiceNo": "1234569",
           "OrderBlocks": [
               {
                   "lineNo": 76,
                   "ProductCode": "#22291"
               }
           ]
       }
   ]
}

Also, as you can see, even though the JSON is big and has multiple nested arrays and objects, it can be read with the same methods as the dataset that we describe above. That’s the magic of JSON.

Test and access JSON properties

Now that we understand JSON and how it can be created, let’s examine a few code snippets commonly asked about in different communities, and we’ll access keys and values within the JSON.

Note on testing vs. inspecting responses: The examples below focus on accessing JSON properties from an API response. In practice, most API tests in Postman use assertions to verify that required properties exist and contain expected values. Accessing JSON properties is the foundation for writing those assertions.

For convenience and speed, you can fork the following Postman collection or click the Run in Postman button below.

Let’s walk through a few examples, where we’ll be using code that uses the pm library to run the test method. The text string will appear in the test output. The function inside the test represents an assertion as given below:

1. An array of all properties in an array of object: In this example, we’ll have an API response body as jsonData and a code snippet showing how to access array properties within an array of object. API response body:

{
   "data": {
       "items": [
           {
               "orderID": "0000001211",
               "orderInvoiceNo": "1234567",
               "OrderBlocks": [
                   {
                       "lineNo": 1,
                       "productCode": "001"
                   },
                   {
                       "lineNo": 2,
                       "productCode": "012"
                   },
                   {
                       "lineNo": 3,
                       "productCode": "013"
                   },
                   {
                       "lineNo": 4,
                       "productCode": "014"
                   }
               ]
           }
       ]
   }
}

Code snippet to get all the properties of objects within an array:

pm.test("array of all properties", () => {
   //get data from API in jsonData
   let jsonData = pm.response.json()
   arrayOfObject = jsonData.data.items[0].OrderBlocks;
 
   //method 1
   let resIdData = arrayOfObject.map(a => a.lineNo);
   console.log("values of lineNo in array : " + resIdData)
 
   //method 2
   let resProductCode = arrayOfObject.map(({ productCode }) => productCode);
   console.log("values of productCode in array : " + resProductCode)
 
});

2. (Find) Array item by property: In this example, we’ll walk through the response body, where we will find array items using JavaScript’s find function, which scans the array and returns the object or value similar to the search term.

{
   "data": {
       "items": [
           {
               "orderID": "0000001211",
               "orderInvoiceNo": "1234567",
               "OrderBlocks": [
                   {
                       "lineNo": 1,
                       "productCode": "001"
                   },
                   {
                       "lineNo": 2,
                       "productCode": "012"
                   },
                   {
                       "lineNo": 3,
                       "productCode": "013"
                   }
               ]
           }
       ]
   }
}

Code snippet that searches for objects having the value “3”:

pm.test("array of all properties", () => {


   let jsonData = pm.response.json()
   arrayOfObject = jsonData.data.items[0].OrderBlocks;
 
   // You can use the arrow function expression:
   var result = arrayOfObject.find(obj => {
       // Returns the object where
       // the given property has some value
       return obj.lineNo === 3
   })
   console.log(result)
});

3. Arrays inside an array of objects: In this example, we’ll access the values that are within an array of objects. API response body:

{
   "data": {
       "items": [
           {
               "orderID": "0000001211",
               "orderInvoiceNo": "1234567",
               "OrderBlocks": [
                   {
                       "lineNo": [
                           1,
                           3,
                           4,
                           5
                       ],
                       "productCode": "001"
                   },
                   {
                       "lineNo": [
                           0,
                           6,
                           2,
                           5
                       ],
                       "productCode": "012"
                   }
               ]
           }
       ]
   }
}

Code snippet:

pm.test("array of all properties", () => {
   let jsonData = pm.response.json()
   arrayOfObject = jsonData.data.items[0].OrderBlocks;
 
   //Accessing Array within array
   var arrWithinArr = arrayOfObject.map(o => o.lineNo)
   console.log(arrWithinArr)
 
   //optional code, if you want to concat all results
   //Concating all the array retrieved from arrayOfObject
   const concatAllArrays = [].concat(...arrWithinArr)
   console.log(concatAllArrays)
});

Conclusion

We’ve just gone through a conceptual overview of the JSON data format and how it gained popularity when XML was at its peak. After exploring how to work with both simple and complex JSON responses, you should now understand how to access JSON properties with Postman. In practice, these JSON access patterns are commonly used as part of automated tests and validation workflows.

Learn more about writing test scripts in Postman →

Technical review by Ian Douglas

What do you think about this topic? Tell us in a comment below.

Comment

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


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

2 thoughts on “How to Test JSON Properties in Postman

    User Avatar

    If we need to filter some Collection data using multiple Conditions with AND and OR Operators.
    I need to know how can we achieve and do the same?

    User Avatar

    Very nice explanation