Skip to content

How To Edit Json Using Postman

API Testing Blog

Editing JSON in Postman: A Comprehensive Guide

Postman is a powerful tool for API testing, and understanding how to manipulate JSON data within it is essential. This guide will walk you through various methods for editing JSON payloads using Postman, from basic modifications to advanced techniques.

1. Editing JSON in the Request Body Directly

The most straightforward way to edit JSON in Postman is by directly modifying the text within the request body. This method is suitable for simple adjustments and quick prototyping.

Steps:

  1. Open the request body: Navigate to the “Body” tab of your request.
  2. Choose “raw” mode: Select “raw” as the body type and specify “JSON” as the format.
  3. Directly edit the JSON: The request body editor will display the JSON text. Modify the values, keys, or structure as needed.

Example:

Original JSON:

{
"name": "John Doe",
"age": 30,
"city": "New York"
}

Modified JSON:

{
"name": "Jane Doe",
"age": 25,
"city": "Los Angeles"
}

2. Using the Postman Form Data Editor

For more structured editing, Postman’s form data editor provides a user-friendly interface. This approach is especially beneficial when you need to modify multiple JSON fields individually.

Steps:

  1. Choose “form-data” mode: In the “Body” tab, select “form-data” as the body type.
  2. Add or modify fields: Postman will present a table where you can add new key-value pairs or edit existing ones.
  3. Select “JSON” as the value type: For each field, ensure the “Value Type” dropdown is set to “JSON.” This enables complex JSON structures as field values.

Example:

Original JSON:

{
"user": {
"name": "John Doe",
"email": "john.doe@example.com"
}
}

Modified JSON:

{
"user": {
"name": "Jane Doe",
"email": "jane.doe@example.com"
}
}

Form data editor:

KeyValue TypeValue
user.nameJSON”Jane Doe”
user.emailJSONjane.doe@example.com

3. Leveraging the Postman Environment Variables

Environment variables provide a dynamic and organized way to manage JSON data within your requests. This is essential for reusing the same request structure with different data sets.

Steps:

  1. Define environment variables: In the “Variables” section of Postman, create environment variables with relevant names and values representing your JSON data.
  2. Utilize variables in the request body: Use double curly braces {{ }} to reference these environment variables within your JSON payload.
  3. Update variables dynamically: You can update the values of these variables before sending each request, allowing for flexible data manipulation.

Example:

Environment Variables:

NameValue
nameJohn Doe
age30

JSON Request Body:

{
"name": "{{name}}",
"age": {{age}}
}

4. Using Postman’s Pre-request Scripts

Pre-request scripts allow you to execute JavaScript code before a request is sent. This opens up advanced possibilities for editing your JSON payload dynamically.

Steps:

  1. Create a pre-request script: Go to the “Pre-request Script” tab of your request.
  2. Write JavaScript code: Employ JavaScript functions and syntax to modify the request body’s JSON data, potentially based on other variables, external calls, or custom logic.
  3. Use pm.request.body.raw() or pm.request.body.json(): These methods provide access to the request body so you can manipulate its content.

Example:

Pre-request Script:

// Replace the age value with a random number between 20 and 40
let age = Math.floor(Math.random() * (40 - 20 + 1)) + 20;
pm.request.body.raw = JSON.stringify({
"name": "John Doe",
"age": age
});

5. Postman Collections & Data Files

For complex scenarios with multiple sets of JSON data, Postman collections combined with data files provide a powerful solution.

Steps:

  1. Create a Postman collection: Group your requests into a collection.
  2. Add a data file: Import a .csv or .json data file containing multiple sets of JSON values.
  3. Configure data iteration: Use the “Data” section within the collection runner to iterate through your data file.
  4. Use pm.iterationData in pre-request scripts: Access data from the current iteration using pm.iterationData to dynamically modify your JSON payload within the pre-request script.

Example:

JSON Data File (data.csv):

name,age,city
John Doe,30,New York
Jane Doe,25,Los Angeles

Pre-request Script:

pm.request.body.raw = JSON.stringify({
"name": pm.iterationData.get("name"),
"age": pm.iterationData.get("age"),
"city": pm.iterationData.get("city")
});

By leveraging Postman’s features for JSON editing, you empower yourself to effectively create and modify complex API requests, test how your APIs handle different data configurations, and ultimately write more reliable and robust test workflows.

API Testing Blog