Site icon TestingDocs.com

What is JsonPath and JsonPath Expressions?

Overview

In this post, we will learn about JsonPath and some examples and JsonPath expressions. To compare, what XPath is for XML, JsonPath is for JSON documents.

What is JsonPath?

JsonPath is Java DSL for reading JSON documents. The root in JsonPath is referred with $ and we can build JsonPath Expressions using the dot-notation.

Maven Dependency

To add JsonPath to Maven based automation projects, we can use the Maven dependency. Modify the version and add the latest version string.

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>Use_Latest_Version</version>
</dependency>

 

Example

Most of the Restful webservices are built with JSON as the payload. (Content-Type=’application/json’). To validate the API calls we should have proper understanding of JsonPath.

We will stick to Karate framework in this post. In Karate for every HTTP call the response is set to the variable response. We can consider the root of the JSON Response document as response or $.

In this example, we have called a GET project API call of Mantis bug tracker. We are validating and matching the actual Project name in the JSON API response with expected name.

If both are same the Test would be marked as Pass in the Test result.

Both the expressions are the same in Karate:

Then match response.projects[0].name == ‘TestingDocsProject’

Then match $.projects[0].name == ‘TestingDocsProject’

JsonPath filters

To validate the response we can use JsonPath expressions. Assert to the actual JSON response to the Expected values. Filter and extract information from the JSON responses. In this example, we have filtered the project name from the JSON response.

Karate Automation Tutorial:

https://www.testingdocs.com/karate-automation-tool-tutorials/

For More Information:

https://github.com/json-path

Exit mobile version