Open In App

How to add nested object to existing JSON using Postman ?

Postman has become a critical tool for developers around the world, particularly when dealing with JSON data. This article will guide you through the process of adding nested objects to an existing JSON using Postman, with a focus on the approaches and syntax involved.

Understanding JSON and Nested Objects:

JSON (JavaScript Object Notation) is a popular data exchange format, thanks to its simplicity and readability. It is often used when data is sent from a server to a web page. A nested object in JSON means an object within an object. It is an effective way to associate related data.

{
"person": {
"name": "John Doe",
"age": 30,
"address": {
"street": "123 Main St",
"city": "Anytown",
"country": "USA"
}
}
}

In the above example, person is an object, and address is a nested object within person.

The Role of Postman:

Postman is a versatile tool used for API testing. It allows developers to send HTTP requests and view responses, making it an ideal tool for dealing with JSON data. Postman can handle both flat and nested JSON structures, although manipulating nested JSON objects requires a slightly different approach, which we will explore in this guide.

Approach 1: Using JSON.parse and JSON.stringify

var jsonData = pm.response.json();
jsonData.newNestedObject = {"key1": "value1", "key2": "value2"};
pm.environment.set("updatedBody", JSON.stringify(jsonData));

Approach 2: Direct Assignment

var jsonData = pm.response.json();
jsonData.newNestedObject = {};
jsonData.newNestedObject.key1 = "value1";
jsonData.newNestedObject.key2 = "value2";

Approach 3: Using a Pre-Request Script

var requestBody = JSON.parse(pm.environment.get("body"));
if (requestBody.info.product !== null) {
    requestBody.info.product = null;
} else {
    var jsonObject = JSON.parse(requestBody);
    jsonObject.product.push({ productId: "123" });
    pm.environment.set("updatedBody", JSON.stringify(requestBody));
}

Creating an Application to Test JSON Manipulation

Step 1: Install Required Modules

Start by installing the required Node.js modules. You will need express for handling HTTP requests and body-parser for parsing JSON data. Install these modules using the following commands:

npm install express
npm install body-parser

Step 2: Update Dependencies in Package.json

After installing the necessary modules, make sure to update your package.json file to reflect these dependencies:

"dependencies": {
"express": "^4.17.1",
"body-parser": "^1.19.0"
}

Step 3: Create the Application

Next, create a new file named app.js and add the following code:

const express = require("express");
const bodyParser = require("body-parser");

const app = express();
app.use(bodyParser.json());

app.post("/test", function (req, res) {
    console.log(req.body);
    res.send("Received");
});

app.listen(3000, function () {
    console.log("Example app listening on port 3000!");
});

Step 4: Test the Application with Postman

Now that the application is ready, you can use Postman to send a POST request to http://localhost:3000/test. Include a JSON body with a nested object and see how the application logs the body of the request.

Steps to Convert Nested Objects to JSON in Postman:

Create a new Request in Postman:

1_11zon-(1)

Enter the Requested URL:


2_11zon-(1)

Select Body:


3_11zon-(1)

Choose JSON Format:

4_11zon-(1)

Enter JSON payload:

5_11zon-(1)

Send the Request:

6_11zon

Verify the Request:

7_11zon

Article Tags :