(This article was originally written by Vikram Sahu with technical review by Ian Douglas)
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 article, we’ll walk through an overview of JSON architecture, structure, and its examples. Then, we’ll check out some code snippets that will help us access and test JSON properties with 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 said earlier, it’s the most commonly used format for data exchange between two different machines running at two different locations geographically, or it can be the two applications running on the same machine.
Though it’s very close to JavaScript object syntax, it can be independently used with different 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 language, JSON is 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, boolean, 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 the user to generate simple to complex datasets without many efforts.
For example, the simplest dataset can be an object starting with {
and ending with }
chaving a key/values pair where keys are name, email id, Twitter, GitHub handle and values are as shown in the object below.
{
"email": "heyjson@example.com",
"name": "JSON",
"twitter": "@callmeJSON007",
"github": "helloworldjsonhere"
}
And the complex dataset can be a combination of objects and arrays, as shown below. The complexity of structure can be increased as per the requirement 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
As we now know about JSON and how it can be created, let’s check a few code snippets which are commonly asked among different communities, and we’ll access keys and values within the JSON.
To make it convenient and quick, you can fork the following Postman collection or click on 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:
-
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)
});
- (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 search for object having 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)
});
- Arrays inside array of objects: In this example, we’ll access the values which 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 its popularity when XML was high in its time. After looking at the similarities and differences between parsing and generating simple versus complex JSON datasets, hopefully you now know how to access and test JSON properties with Postman.
The post How to Test JSON Properties in Postman appeared first on Postman Blog.
Top comments (1)
Amazing article @sahuvikramp