Skip to content

Can Postman Be Used For Json

API Testing Blog

Can Postman Be Used for JSON?

Postman is a powerful API platform that offers a wide range of features for testing APIs, including extensive support for JSON. In this guide, we’ll explore how Postman can be effectively used to handle JSON in API testing, covering various aspects of working with this data format.

Sending JSON Data in Requests

One of the primary uses of Postman is sending JSON data in API requests. Let’s see how this is done:

1. Setting the Content-Type:

  • When sending JSON data, you need to set the Content-Type header to application/json. This tells the server that the data being sent is in JSON format.

2. Creating a JSON Body:

  • In the Postman request body, choose the “raw” tab and select “JSON” from the dropdown.
  • You can directly type your JSON data or paste it from another source.

Example:

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

3. Sending the Request:

  • Click the “Send” button to execute the request. Postman will automatically convert your JSON data into the appropriate format for the server.

Reading JSON Responses

Postman also excels in handling JSON responses from APIs.

1. Understanding JSON Response Structure:

  • When an API responds with JSON data, Postman displays it in a user-friendly format, making it easy to understand the response structure.

2. Accessing Response Data:

  • You can access specific values within the JSON response using the “Body” tab or the “Test” tab.
  • The “Body” tab shows the raw JSON response, while the “Test” tab allows you to write scripts to extract values and perform assertions.

Example:

pm.test("Verify name is John Doe", function() {
var jsonData = pm.response.json();
pm.expect(jsonData.name).to.be.equal('John Doe');
});

Example: Creating a Test for a JSON API

Let’s create a complete test using Postman for a hypothetical API that creates a new user:

1. Request Setup:

  • Create a new POST request in Postman.

  • Set the URL to https://api.example.com/users.

  • Set the Content-Type header to application/json.

  • Add the following JSON data in the request body:

    {
    "name": "Alice Smith",
    "email": "alice@example.com"
    }

2. Test Script:

  • In the “Test” tab, write the following script:

    pm.test("Status code is 201", function() {
    pm.response.to.have.status(201);
    });
    pm.test("Response contains user ID", function() {
    var jsonData = pm.response.json();
    pm.expect(jsonData.id).to.be.a('number');
    });

3. Execution:

  • Send the request. Postman will execute the tests and display the results.

Validating JSON Data

Postman provides powerful tools for validating JSON responses against expected schemas.

1. Using Schemas:

  • You can define a JSON schema to describe the structure of the expected response.
  • Postman can validate the actual response against this schema, ensuring data integrity.

2. Schema Validation in Tests:

  • You can integrate schema validation directly into your test scripts using the pm.expect() function.

Example:

var schema = {
"type": "object",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" }
},
"required": ["id", "name"]
};
pm.test("Response matches the schema", function() {
pm.expect(pm.response.json()).to.matchSchema(schema);
});

Parsing and Manipulation

Postman’s scripting capabilities allow you to parse, manipulate, and extract specific data from JSON responses.

1. Using JavaScript:

  • You can write JavaScript code within the “Test” tab to parse and work with JSON data.

2. Accessing Data with pm.response.json():

  • The pm.response.json() function converts the JSON response into a JavaScript object, allowing you to easily access data.

Example:

pm.test("Extract user name", function() {
var jsonData = pm.response.json();
var userName = jsonData.name;
console.log("User name:", userName);
});

Conclusion

Postman is a versatile tool for working with JSON in API testing, offering a rich set of features for sending, receiving, validating, and manipulating JSON data. By leveraging these capabilities, you can enhance the quality and efficiency of your API testing process.

API Testing Blog