Table of Contents
Introduction
JSON (JavaScript Object Notation) has become the most popular data format for web APIs and microservices. Its lightweight structure, human readability, and ease of parsing make JSON an ideal transport format for the modern web and any JSONPath Tester.
However, validating and extracting data from complex JSON responses can be challenging, especially when testing APIs. This is where JSONPath comes in. JSONPath is а query language that allows easy traversal and manipulation of JSON documents. With its simple but powerful syntax, JSONPath helps QA teams and testers extract and validate JSON data with precision during API testing.
In this article, we will explore how JSONPath can help simplify two key aspects of working with JSON APIs – data extraction and data validation.
The Need for Precise JSON Data Selection
As APIs continue to proliferate, the JSON responses they produce are getting larger and more complex. A single API response may contain nested objects, arrays of elements, deeply nested data, and more. Manually analyzing or validating such responses is tedious and error-prone.
Testers need а way to pinpoint and extract specific portions of the JSON data precisely. For example, to validate that а user’s first name in а response matches what was passed to the API. Or to check if а numeric ID value falls within an expected range.
This is where JSONPath comes into the picture.
Introducing JSONPath
JSONPath provides SQL-like syntax to traverse JSON documents and select data from them without needing to write lengthy loops or complex iteration code.
Some examples of what can be done using JSONPath:
- Easily select all ids from an array of objects
- Extract just the titles from а response containing blog posts
- Validate nested data such as profile.emailAddress
In short, JSONPath allows testers to query their JSON test data with simple path expressions instead of having to deal with the underlying complexities.
JSONPath Syntax overview
The JSONPath syntax consists of the root object $, paths that traverse the JSON structure using . or [] operators, and а wealth of utility functions for data manipulation.
Here is а quick example that extracts an array of ids from а response:
$.employees[*].id
This expression traverses to the employees array (-> employees[*]), then grabs the id property of each object within it (-> *.id). The result is an array of ids.
Other commonly used JSONPath operators and functions include:
- Bracket notation [] for array access
- Filter expressions such as [?(@.age > 18)]
- Wildcards for pattern matching
- Length function for getting array lengths
- Slice function for partitions
JSONPath sets itself apart from regular JSON traversal APIs in two ways – succinct syntax that avoids loops and conditionals, and utility functions tailored for the needs of testers and QA teams.
With this versatility, testers can extract their desired data concisely without needing to write lengthy data manipulation code.
Using JSONPath for Data Validation
Validating JSON responses against expected values is one of the most common test cases for API testing. JSONPath can help streamline the validation process in two ways:
- Value comparison using JSONPath expressions
- JSON schema validation
Value Comparison
JSONPath allows comparing а selected value against an expected value using standard assertions. For example:
Assert.equals(25, $.user.id)
The id property from the JSON data is extracted using а JSONPath expression, then compared to the expected value. If the values don’t match, the assertion would fail signaling an issue.
All common matchers like equals, contains, matchesRegex, lessThan etc. can be used by extracting JSON values using JSONPath.
JSON Schema Validation
For more comprehensive JSON validation, JSON Schema provides а standardized way of defining the structure of expected JSON data. JSONPath works seamlessly with JSON Schema validation allowing entire response structures to be validated automatically.
For example, the schema:
{
“$schema”: “http://example.com/schema#”,
“type” : “object”,
“properties” : {
“userName” : { “type” : “string”},
“email” : { “type” : “string”}
}
}
Can be used with JSONPath enabled test tools to validate responses like:
{
“userName” : “john”,
“email” : “john@email.com”
}
If the response structure doesn’t adhere to the defined schema, the validation would fail throwing an error.
Integrating JSONPath into API Tests
While JSONPath is extremely useful, it needs to be integrated with testing tools to unlock its full potential for API testing. Testing frameworks like Postman, Rest Assured, Karate, and LambdaTest Kane enable seamless integration of JSONPath for enhanced productivity.
1. Enabling JSONPath Based Assertions
Most API testing platforms allow creating JSONPath based assertions for response validation using а friendly UI without needing to write code.
For example, а validation rule can be created visually by specifying the JSONPath expression, expected value, matcher, and optional error message. The platform code internals would use JSONPath libraries to implement the actual assertion.
2. Extracting and Storing JSON Values
Running data-driven testing with dynamic parameter values often requires first extracting values from previous responses to set up the test data for subsequent requests.
For example, extracting an auth token from the login API response to use in subsequent authenticated requests.
JSONPath simplifies this by extracting and storing response data with expressions like:
*.authToken
The extracted value can then be referenced across requests using variables enabling seamless data-driven testing.
3. Generating Code Snippets with JSONPath
To smoothen collaboration between teams, testing tools allow exporting API tests into code across multiple languages – Java, JavaScript, Python, etc.
These code snippets contain implemented JSONPath assertions and data extraction logic that developers can seamlessly integrate into their frameworks.
This helps bridge the gap between testers and developers by fostering collaboration.
Steps for Using JSONPath in API Testing
The following are the steps:
1. Understand the JSON Structure
Familiarize yourself with the JSON response to identify the data points you need to extract. For example:
{
“user”: {
“id”: 123,
“name”: “John Doe”,
“email”: “john.doe@example.com”,
“address”: {
“city”: “New York”,
“zip”: “10001”
},
“orders”: [
{
“id”: 1,
“total”: 50.5,
“status”: “shipped”
},
{
“id”: 2,
“total”: 20.0,
“status”: “pending”
}
]
}
}
2. Define JSONPath Queries
JSONPath expressions help pinpoint specific parts of the JSON. Common examples:
- $: The root object/element.
- $.user.name: Extracts the name of the user (John Doe).
- $.user.address.city: Extracts the city (New York).
- $.user.orders[*].status: Extracts all order statuses ([“shipped”, “pending”]).
- $.user.orders[?(@.status == “pending”)]: Filters orders with а pending status.
3. Implement JSONPath in API Testing
Use tools like Postman, RestAssured (Java), or Python for API testing:
Using Postman:
- Access the response in the Tests tab.
- Use а library like jsonpath to validate data:
let jsonData = pm.response.json();
let statuses = jsonpath.query(jsonData, ‘$.user.orders[*].status’);
pm.test(“Pending status exists”, function () {
pm.expect(statuses).to.include(“pending”);
});
Using Python:
- Use the jsonpath-ng or jsonpath-ng.ext library:
from jsonpath_ng.ext import parse
data = {
“user”: {
“id”: 123,
“name”: “John Doe”,
“email”: “john.doe@example.com”,
“address”: {
“city”: “New York”,
“zip”: “10001”
},
“orders”: [
{“id”: 1, “total”: 50.5, “status”: “shipped”},
{“id”: 2, “total”: 20.0, “status”: “pending”}
]
}
}
jsonpath_expr = parse(‘$.user.orders[?(@.status == “pending”)].id’)
result = [match.value for match in jsonpath_expr.find(data)]
print(result) # Output: [2]
Using RestAssured (Java):
- Use the JsonPath class:
import io.restassured.path.json.JsonPath;
String json = “{…}”; // Your JSON string
JsonPath jsonPath = new JsonPath(json);
List<Integer> orderIds = jsonPath.getList(“user.orders.findAll { it.status == ‘pending’ }.id”);
System.out.println(orderIds); // Output: [2]
4. Validate Extracted Data
- Compare the extracted values against expected values.
- Assertions depend on the framework being used. Examples:
- Postman: pm.expect(value).to.eql(expectedValue);
- Python: assert value == expected_value
- Java: Assert.assertEquals(value, expectedValue);
5. Automate Testing
- Integrate JSONPath-based extraction and validation into your test suite.
- Ensure that API responses conform to requirements across environments.
LambdaTest: Features and Applications of JSONPath in API Testing
LambdaTest is а сloud-based сross-browser testing рlatform that enables users to test their web aррliсations and websites across а wide range of browsers, oрerating systems, and deviсes. It supports manual, automated, and visual testing, offering robust tools for develoрers and QA teams to ensure their aррliсations work seamlessly across diverse environments.
When it comes to JSONPath and its aррliсation in LambdaTest, the focus is primarily on using JSONPath for validating API responses during API testing and integration with test automation frameworks.
Key Features of LambdaTest:
● API Testing and Validation
LambdaTest allows users to validate the functionality of APIs that power web applications.
JSONPath expressions can be used to extract and validate specific data points in JSON responses.
● Integration With Automation Frameworks
LambdaTest integrates with popular automation frameworks like Selenium, Cypress, Playwright, and TestNG. Users can leverage JSONPath expressions within these frameworks to automate API response validation.
● Cloud-Based Execution for API Testing
LambdaTest provides а cloud-based infrastructure where API tests can be executed alongside UI tests. JSONPath queries can validate API responses as part of an end-to-end testing pipeline.
● Integration With CI/CD Tools
LambdaTest supports integration with CI/CD pipelines like Jenkins, GitHub Actions, and GitLab CI/CD. During automated test execution in CI/CD workflows, JSONPath can validate dynamic API responses, ensuring proper data flow across microservices.
Rapid API Testing with LambdaTest Kane
LambdaTest Kane is an intelligent test orchestration platform that combines the power of JSONPath with cutting-edge test automation capabilities.
With Kane, testers can rapidly validate the structure and output of JSON APIs without writing any code. Its intuitive visual interface makes it easy to:
- Create JSONPath powered response assertions
- Extract dynamic parameter values from responses
- Generate test automation code for CI/CD integration
- Run API testing at scale on the cloud Selenium grid
Kane also utilizes AI and ML to continuously improve test maintenance and accelerate release cycles.
By integrating robust JSONPath capabilities into its easy-to-use automation platform, Kane helps QA teams scale rapid API testing and validation. With its library of over 3000+ built-in assertions covering security, compliance, and functional validation across 120+ protocols, Kane provides the most comprehensive quality gate.
Conclusion
As web APIs continue to power the modern application landscape, using а versatile JSON query language like JSONPath is key to simplifying how testers and QA teams interact with JSON data.
With its succinct syntax and deep capabilities, JSONPath solves two vital workflow needs – flexible data extraction and robust validation for API testing. When combined with cloud-based test automation platforms like LambdaTest Kane, JSONPath helps organizations accelerate API quality assurance and testing velocity.
Also Read: The Benefits Of Partnering With A Full-Service Agency