banner



How To Send Byte Array In Json Postman

How to Test JSON Properties in Postman

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

In this blog postal service, we'll walk through an overview of JSON compages, structure, and its examples. Then, nosotros'll check out some code snippets that will help us access and exam JSON properties with Postman.

JSON compages

JSON is a text-based data format that'due south used to represent data in a structured way based on JavaScript object syntax. As said earlier, it's the near commonly used format for information substitution between 2 different machines running at 2 different locations geographically, or it tin can be the two applications running on the same machine.

Though it's very shut to JavaScript object syntax, it can exist independently used with dissimilar programming languages. Due to its popularity and man-readable standards, nigh programming languages accept started supporting the JSON format within their pre-built libraries, which helps users to create or parse JSON data.

JSON information tin can be easily stored in a file with a file extension chosen .json and MIME type as application/json. JSON supports the ii most used information structures, arrays and objects. Since these information structures are foundational pillars of modern programming linguistic communication, JSON is strong and useful data-substitution format.

JSON structure

As described to a higher place, JSON structure is generated using arrays and objects. This means well-nigh all the bones 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 examination the data with their other software programs. Since JSON is human being-readable, it allows the user to generate elementary 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 proper noun, e-mail id, Twitter, GitHub handle and values are as shown in the object below.

{    "e-mail": "[e-mail protected]",    "name": "JSON",    "twitter": "@callmeJSON007",    "github": "helloworldjsonhere" }        

And the complex dataset tin be a combination of objects and arrays, equally shown below. The complication of construction tin exist increased as per the requirement and accessibility.

{    "items": [        {            "orderID": "0000001211",            "orderInvoiceNo": "1234567",            "OrderBlocks": [                {                    "lineNo": [1,4,6,viii,nine,1,4],                    "ProductCode": "#31451"                },                {                    "lineNo": ii,                    "ProductCode": "#64311"                },                {                    "lineNo": iii,                    "ProductCode": "#85959"                }            ]        },        {            "orderID": "0000001212",            "orderInvoiceNo": "1234568",            "OrderBlocks": [                {                    "lineNo": seven,                    "ProductCode": "#86869"                },                {                    "lineNo": [6,7,iv,8,4,two],                    "ProductCode": "#10384"                },                {                    "lineNo": 12,                    "ProductCode": "#00873"                }            ]        },        {            "orderID": "0000001213",            "orderInvoiceNo": "1234569",            "OrderBlocks": [                {                    "lineNo": 76,                    "ProductCode": "#22291"                }            ]        }    ] }        

Also, every bit you tin meet, even though the JSON is large and has multiple nested arrays and objects, it can be read with the aforementioned methods as the dataset that we depict above. That'due south the magic of JSON.

Test and access JSON properties

As we at present know most JSON and how information technology tin can be created, permit'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 information technology convenient and quick, you tin fork the post-obit Postman collection or click on the Run in Postman button below.

Allow'due south walk through a few examples, where we'll exist using lawmaking that uses the pm library to run the test method. The text string will appear in the test output. The role within the exam represents an assertion every bit given below:

1. An array of all properties in an array of object: In this example, nosotros'll take an API response body as jsonData and a lawmaking snippet showing how to access array backdrop inside an assortment of object. API response torso:

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

Code snippet to become all the properties of objects within an assortment:

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

2. (Find) Array item past property: In this example, nosotros'll walk through the response body where we will discover array items using JavaScript'south find office, which scans the array and returns the object or value like to the search term.

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

Code snippet that search for object having value "3":

pm.examination("array of all properties", () => {      let jsonData = pm.response.json()    arrayOfObject = jsonData.data.items[0].OrderBlocks;      // You tin utilize the arrow function expression:    var result = arrayOfObject.find(obj => {        // Returns the object where        // the given property has some value        render obj.lineNo === 3    })    panel.log(result) });        

3. Arrays inside assortment of objects: In this example, we'll admission the values which are within an array of objects. API response trunk:

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

Lawmaking snippet:

pm.test("array of all backdrop", () => {    allow jsonData = pm.response.json()    arrayOfObject = jsonData.data.items[0].OrderBlocks;      //Accessing Array within assortment    var arrWithinArr = arrayOfObject.map(o => o.lineNo)    console.log(arrWithinArr)      //optional code, if you desire 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 information technology gained its popularity when XML was high in its fourth dimension. After looking at the similarities and differences between parsing and generating simple versus complex JSON datasets, hopefully you now know how to access and examination JSON backdrop with Postman.

Technical review past Ian Douglas

Source: https://blog.postman.com/how-to-test-json-properties-in-postman/

0 Response to "How To Send Byte Array In Json Postman"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel