- 2 minutes to read

Formula - jsonPath

The jsonPath Formula function returns the values in JSON content that match a JSONPath expression.

Syntax

jsonPath('expression', content)

Parameters

Parameter Required Description
'expression' Yes A JSONPath expression in single quotes. The leading $. is optional: 'Seq' and '$.Seq' select the same property.
content Yes The JSON text: body, context, or the result of another function (see Formula language rules).

Returns

The value of every node that matches, one value each; duplicates are collapsed. When several content values come in, such as from context(regex(...)), each is parsed and all matches are returned. When nothing matches, nothing is returned.

To return property names instead of values, use jsonPathKey.

Examples

Example 1: Read a property from the body

Input

{ "Seq": 2 }

Formula

jsonPath('Seq', body)

Result

2

Formula jsonPath expression returning the Seq value from a JSON message body in the Test Expression tab
'Seq' without $. selects the top-level Seq property.

Example 2: Read every Id in an array

Input

[
{ "Id": 0 },
{ "Id": 1 },
{ "Id": 1 },
{ "Id": 2 },
{ "Id": 3 }
]

Formula

jsonPath('$..Id', body)

Result

0
1
2
3

Formula jsonPath recursive descent returning four unique Id values from a JSON array in the Test Expression tab
$..Id (recursive descent) finds every Id at any depth; the duplicate 1 is collapsed, so the test shows four unique matches.

Example 3: Read a property from a context value

Input

Key Value
id {"Seq":2}

Formula

jsonPath('Seq', context('id'))

Result

2

Formula jsonPath expression reading Seq from the id context property in the Message Context tab
The JSON stored in the id context property is parsed like a message body.

Example 4: Read Ids from several context properties

Input

Key Value
id [{ "Id": 0 },{ "Id": 1 },{ "Id": 1 },{ "Id": 2 }]
id2 [{ "Id": 3 },{ "Id": 4 },{ "Id": 5 },{ "Id":6 }]

Formula

jsonPath('$..Id', context(regex('id.*')))

Result

0
1
2
3
4
5
6

Formula jsonPath over two context properties returning seven unique Id values in the Test Expression tab
context(regex('id.*')) returns the values of both id and id2; jsonPath parses each and returns seven unique values.

Notes

  • Property names in the expression are matched with their exact case: Seq and seq are different properties.
  • A literal ' in a property name is written ''; see Formula language rules.
  • When one JSONPath on the body or a single context property is all you need, the JSON Path plugin does the same without a formula; use jsonPath to nest, combine, or aggregate. See Choose a plugin.

Next Steps