How has KarateDSL simplified JsonPath evaluation?

Welcome back, TestCommunity!!!

First and foremost, I’d like to ask you all “how you’re doing and how things are going at your end ?”

I hope you’re enjoying my Karate DSL blog series, which aims to cover all the key features offered by this fabulous tool. Today we’ll look at methods for extracting json objects/values from complex payloads.

Let’s start with the fundamentals of JSONPath before moving on to advanced concepts.

What is JSONPath ?

JSONPath is a query language for JSON which is used for selecting and extracting a JSON document’s property values.

The following JSONPath syntax rules should be followed in order to extract desired JSON objects:

  • The root object or element is denoted by the “$” symbol.
  • The “.” dot operator is used to denote a child element of the current element.
  • The “..” operator is used for deep scan any elements by name in entire JSON payload
  • The “@” symbol corresponds to the current entity or element.
  • The subscript operator “[ ]” is used to indicate a child element of the currently selected element (by name or index).
  • The “*” operator returns all objects when used as a wildcard.
  • “[? (<expression> )]” to get a list of all items that match a set of criteria by returning boolean value.

For more details on JSONPath refer this link.

JSONPath Implementation in KarateDSL

Karate allows you to elegantly extract the desired object’s JsonPath from a complex API response, let us see how can we achieve this ?

We have to first download latest maven dependency for JsonPath:

Jayway has it own JsonPath evaluator, we may use this to ensure that the json paths are accurate.

Stated API is used in all of the examples below-


For more open-source APIs click on this link.

Example 1: Get an objects present in “ebook” array.

Example 2: Get “lccn” key on the 0th index of “identifiers” array.

Example 3: Get all the “name” keys from “subjects” error excluding “url”.

Please note: In karate, you can retrieve a few keys (rather than the entire object) from a JSON array or an object, this can be achieved by using square brackets along with comma(,) separated key names in karate.jsonPath method.

Example 4: Obtain all subjects with the word “Grade” in their names.

We are using regex to match characters.

Example 5: Get subject url present on the last index and subject names on the last to index of the array

Example 6 : Get JSON object based on indexes range.

We can use StartIndex : DesiredEndIndex which mean start from index 1 (inclusive) until index 2 (exclusive)

Example 7: Get size of the array by using karate.jsonPath method

Method 1: The traditional method of obtaining the size is to extract the array into a single variable and then transfer it to karate.sizeOf() is a method.

Method 2: We can also shorten the line of codes by passing karate.jsonPath() method inside karate.sizeOf() method, it will give you the same result:

Example 8: There are times when dynamically obtaining values from a JSON response is needed. We’ll see how to get the value of the “url” key by passing the subject name in the example below.

Method 1: To get relevant objects, you can transfer a hardcoded value into a variable and use in the jsonPath method.

Method 2: By following the steps below, you can make it absolutely dynamic:

Note: If you do not pass “.url” in the jsonPath it will return entire JSON object which matches passed variable condition.

Example 9: Get the JSON objects excluding an object based on a condition.

Below example shows steps to get all JSON objects from subjects array excluding name that matches ‘Juvenile fiction’.

I’ll end this blog now, hoping that it has helped you understand the numerous methods for extracting desired objects from even the most complex JSON responses returned by APIs. In the next blog, we’ll look at different forms of assertions and matches.

Please share your thoughts in the comments section below and click the Follow button if you find this blog useful. I hope to see you again in the upcoming posts. You may also connect with me on LinkedIn.

Until then, stay safe and strong; we’ll make it through this!!

Happy Learning!!!

QA Lead @ MyGlamm | QA Automation engineer @ Ex-Paytm Insider| Ex-Automation Tester @ Reliance Jio Infocomm Ltd. | Ex-Software Developer @TCS