Skip to content
JMeter JMesPath Extractor

JMeter JMesPath Extractor

Extracting values from server responses is a common operation in scripting scenarios for performance testing. In most cases it is a mandatory step in order to reproduce realistic traffic using dynamic values generated on server side like sessionIds or Tokens.

The most known way to extract values is using regular expression. While regular expressions are efficient for raw text-based responses, they are less efficient for structured responses like XML or JSON. JMeter provides specific extractors for those kind of responses. For JSON structures, JMeter implements 2 ways for handling them:

OctoPerf is JMeter on steroids!
Schedule a Demo

What's JMesPath?

JMesPath is a query language for JSON, precisely specified and explained on its official web site. It enables users to effortlessly navigate, filter, and transform JSON structures, making it an invaluable tool for data extraction and manipulation in performance testing scenarios.

Let's clarify with an example. Given the following JSON:

{
  "user": {
    "name": "John Doe",
    "address": {
      "street": "123 Main St",
      "city": "Anytown"
    }
  }
}

To extract the city attribute, the JMESPath expression would be:

@.user.address.city

Why another expression language for JSON?

JSONPath is a useful query expression and is widely known. However, its behavior varies depending on the platform/language on which it is executed. Even for Java there are many implementations and some features are not present in all of them. JMESPath addresses this issue with its complete specification and precise grammar. It provides a more intuitive syntax compared to JSONPath, making it easier to use.

However, JMesPath comes with some limitation that are discussed later in this article (Limitation section).

As a summary this table shows a comparison between both languages:

Feature JMESPath JSONPath
Syntax Generally simpler and more readable Can be more complex, especially for beginners
Functionality Offers a rich set of functions for data manipulation Functionality might require combining multiple expressions
Recursive Traversal Limited, requires specifying the full path Can be achieved using wildcards ($..)
Parent Node Referencing Not supported Supported in some implementations

The outcome of this comparison is that JMesPath and JSONPath are both complementary, and leveraging their strengths based on the context is ideal.

How to Use JMesPath in JMeter?

JMesPath is natively supported in JMeter since release 5.2. It can be used for extracting (on the Post Processors submenu) values from server responses or in assertions. Both can be applied on a sampler in order to extract a value from server response or validate it against certain criteria. The following screen shot shows an example of our demo script with a dummy sampler on which a JMesPath Extractor and a JMesPath Assertion are applied.

jmespath-overview

JMesPath Assertions

The assertion allows to validate the server response against defined patterns. The user interface is shown in the screenhost above. The assertion interface requires to set some attributes that are described in JMeter documentation:

Attribute Description Required
Assert JMESPath exists The JMESPath expression specified here should return a value to make the assertion succeed. Yes
Additionally assert value Tells JMeter whether to check the extracted value using the JMESPath expression against the expected value. No
Match as regular expression Tells JMeter whether the expected value is a regular expression. No
Expected Value Value to use for exact matching or regular expression if Match as regular expression is checked No
Expect null Select checkbox if you expect the value to be null No
Invert assertion Invert assertion (will fail if above conditions met) No

So according to the documentation, the assertion allows to check if the JMesPath expression returns a value from the server response. It also allows validating the returned value against either a simple string or a regular expression.

JMesPath Extractor

This Post Processor allows extracting values from server response. The interface looks as shown below:

JMesPath Extractor

The attributes are documented by JMeter guide as follows:

Attribute Description Required
Name Name shown in the tree. No
Apply to On which content the JMesPath should be applied. Can be the main sample only, Sub-samples only, both main sample and sub-samples or a JMeter Variable which requires providing the variable name. Yes
Name of created variable The name of the JMeter variable in which to store the result. Yes
JMESPath expressions The JMESPath expression to use for the extraction. Yes
Match No. If the JMESPath query returns a collection, the extraction behaviour can be specified as follows :
- "0": extracts a random value from the collection
- "-1" extracts all results (default value), they will be named as _N (where N goes from 1 to Number of results)
- N: extracts the Nth result. If N is greater than the number of matches, then nothing is returned. Default value will be used
No
Default Value Default value returned when no match found. It is also returned if the node has no value and the fragment option is not selected. No

The returned value with a JMesPath expression can be either a simple string, a single JSON object or a collection of either string or JSON objects.

In the case of table the extractor allows to choose between getting all the values in a JMeter table variable in which case you need to set the Match No. field to -1. If a random value is required this field should be set to 0 otherwise the rank of required occurrence needs to be specified. Filtering values of a table, can be done in the JMesPath expression.

JMeter Example

In order to handle JSON response the demo script implements a Dummy Sampler that generates a JSON response on which JMesPath expression are applied to extract values:

JMeter Script Dummy Sampler

So let's look at this first expression (exposed on the JMesPath official site) that allows extracting only name of cities belonging to WA state, sorting them then joining them in single JSON attribute named Washington cities:

locations[?state == 'WA'].{state:state, name:name}

As JMeter natively supports JMesPath it allows testing the expression from the "View Results Tree" listener as illustrated on the screenshot below:

JMeter Example

This example illustrates how to filter values from a collection, sort them and build a new JSON object concatenating all the elements of filtered results in a single comma separated string.

JMesPath on OctoPerf

JMesPath extractor is natively supported on OctoPerf since v14.4.1. The interface helps build the JMesPath expression based on the server's response as shown below:

OctoPerf JMesPath Extractor

The JSON is formatted and with a simple checkbox you can choose the element to extract, the expression is then built automatically and shown on the green button.

The expression can be tested on the recorded response (if the script have been generated from an HAR) or on a recent execution as shown below:

OctoPerf Test Extractor

These features are available for free accounts together with the HAR import feature allowing the navigation and comparison between recorded requests/responses and replay ones.

JMesPath main feature

We've seen so far example of expressions and how to use them on JMeter (or OctoPerf). Let's list the main feature JMesPath Offers. The expression language mainly helps extract values from a JSON source. But you can also apply some processing on the extracted value. Here is a list of what can be done;

  • basic expressions: select an element with a precise path to its key

  • projections: allows handling collections with one of the following operations:

  • List projections: using the wild card to select all the elements of a list

  • Slice projections: using expression to select some elements of a list based on their order
  • Object projections: using wild card to select any JSON object
  • Flatten projections: helpful for flattening a hierarchy of objects (like flatting a map)
  • Filter projection: selecting some elements based on specified criteria

  • Pipe expressions: The allowing to chain operations on the result of the precedent expressions

  • Multiselect: Allows to create new JSON structure with elements of the JSON source.

  • Functions:

All these concepts are well explained on the JMesPath official tutorial with some detailed example that you can even modify and test.

Lets consider a more complex example with the JSON below

{
  "order_id": "ORD-123456",
  "user_id": 10,
  "created_at": "2024-04-08T14:19:23+02:00",
  "billing_address": {
    "name": "John Doe",
    "address_line1": "123 Main St",
    "city": "Anytown",
    "state": "CA",
    "postal_code": "12345",
    "country": "US"
  },
  "shipping_address": {
    "name": "John Doe",
    "address_line1": "123 Main St",
    "city": "Anytown",
    "state": "CA",
    "postal_code": "12345",
    "country": "US"
  },
  "items": [
    {
      "id": 1,
      "name": "T-Shirt",
      "price": 19.99,
      "image_url": "https://example.com/images/t-shirt.jpg",
      "quantities": [
        { "size": "Small", "quantity": 3 },
        { "size": "Medium", "quantity": 4 } 
      ]
    },
    {
      "id": 2,
      "name": "Coffee Mug",
      "quantities": [{"size":"uniq", "quantity":2}],
      "price": 8.50,
      "image_url": "https://example.com/images/mug.jpg"
    }
  ],
  "subtotal": 47.48,
  "discount": 5.00,
  "total": 42.48,
  "shipping_cost": 5.00,
  "grand_total": 47.48,
  "payment_method": "Visa",
  "payment_confirmation_code": "ABC123"
}

We can try to extract and flatten all the quantity values with the following expression

items[].quantities[]

The result is:

[
  3,
  4,
  2
]

We can now handle the table with some math function like min, max or sum. Let's try the sum

items[].quantities[].quantity|sum(@)

The result is 9. The pipe allows here to process the result, in this case à table, and apply a function that accepts a flat array. the @ denotes the current node.

Let's now create a new JSON object with the list of item by selecting only the name and price:

items[].{"name":"name","price":"price"}

The result is :

[
  {
    "name": "T-Shirt",
    "price": 19.99
  },
  {
    "name": "Coffee Mug",
    "price": 8.5
  }
]

Limitations

The JMesPath lacks some useful function such as:

  • Arithmetic operation on fields: for example calculating the total price of the cart from the previous JSON is not possible, as the current implementation doesn't allow multiplying the quantity by the unit price.
  • Parent Node Referencing: in JSONPath expression language the "$" references the root of the JSON object and then you can reference its attributes. JMesPath doesn't implement this feature.
  • Recursive traversal: JSONPath implements the ".." wild car which allows recursively diving into the JSON tree and capturing any elements that matches the expression. This feature is not implemented in JMesPath.

There's a fork of JMestPath called community version that implements some of these features such as arithmetic operations, but it's not integrated into the official JMesPath implementation and thus not available in JMeter.

Despite those limitation JMesPath provides a solution for the most common needs of a performance test script.

Conclusion

The JMesPath is an alternative to JSONPath, both provided by JMeter. It comes with some advantages like a complete specification and clearer syntax which make it more attractive for common use. JSONPath is still useful in some cases that are not yet covered by JMesPath.

And as a final note, don't forget you can download the above example from this link.

Want to become a super load tester?
Request a Demo