30/10/2024
Postman is an incredibly powerful tool for API testing, and one of the key features it offers is the ability to extract specific values from JSON responses. When dealing with nested JSON objects, you might find it a bit challenging to access the information you need directly. In this explanation, we’ll break down the steps for extracting values in a straightforward manner.
Before we jump into extracting values, let’s clarify what a nested JSON object looks like. Here’s a simple example:
{ "user": { "id": 1, "name": "John Doe", "address": { "street": "123 Main St", "city": "Anytown", "zipcode": "12345" } } }
In this example, the address
object is nested within the user
object. To extract the information, we need to know how to navigate this structure.
Make a Request:
Begin by making the API request that returns the JSON response. For example, you might perform a GET
request to an endpoint that returns user data.
Access the Test Scripts Section: After receiving a response, navigate to the "Tests" tab in Postman. This is where you'll write your JavaScript code to extract values from the JSON.
Parse the JSON Response:
Use Postman’s built-in method to parse the response body. You can do this by accessing the responseJson
object:
let responseJson = pm.response.json();
Extract Values Using Dot Notation or Bracket Notation: You can extract values from a nested JSON object using dot notation or bracket notation. Here’s how to retrieve various values from our example JSON:
To get the user's name:
let userName = responseJson.user.name;
To get the street address:
let userStreet = responseJson.user.address.street;
If your key contains special characters or spaces, you can use bracket notation:
let userZipCode = responseJson.user.address["zipcode"];
Saving Extracted Values as Environment Variables: You can store the extracted values as environment variables for use in subsequent requests. This way, you can reference them later in other parts of your Postman workflow. For example:
pm.environment.set("userName", userName); pm.environment.set("userStreet", userStreet);
Accessing Stored Variables: In the headers or body of your next requests, you can access these values using Postman’s variable syntax:
{{userName}}
{{userStreet}}
Here’s a complete example test script that encapsulates the above steps:
let responseJson = pm.response.json(); // Extract values let userName = responseJson.user.name; let userStreet = responseJson.user.address.street; let userZipCode = responseJson.user.address["zipcode"]; // Save to environment pm.environment.set("userName", userName); pm.environment.set("userStreet", userStreet); pm.environment.set("userZipCode", userZipCode); // Optional: Log values to console for verification console.log("User Name:", userName); console.log("User Street:", userStreet); console.log("User Zip Code:", userZipCode);
With this approach, you can easily navigate and extract values from nested JSON objects in Postman, making your API testing more efficient and effective!
30/10/2024 | API Testing
30/10/2024 | API Testing
30/10/2024 | API Testing
30/10/2024 | API Testing
30/10/2024 | API Testing
30/10/2024 | API Testing
30/10/2024 | API Testing