Query JSON with JMESPath#
JMESPath is a query language for JSON, that allows elements within a JSON document to be extracted and transformed.
jmespath()
is a custom method that allows searches on JSON objects using the JMESPath query language.
Before you begin#
- Learn more about JMSEPath
- Review the JEMSPath documentation
- JMESPath Specification provides a list of supported expressions
- JMESPath JavaScript library
- JMESPath list and slice projections
- JMESPath object projections
- JMESPath Multiselect
Syntax#
Javascript method:
1 |
|
Python method:
1 |
|
Example#
To help understand what the method does, here is the equivalent longer JavaScript:
1 2 |
|
Note
Expressions must be single-line
Arguments#
Argument | Description |
---|---|
object |
A JSON object, such as the output of a previous node. |
searchString |
An expression written in the JMESPath query language. |
Additional information#
Search parameter order#
Ensemble supports the JMESPath JavaScript library which puts search parameters in order search(object, searchString)
which differs to those explained on official JMESPath pages:
* JMESPath Tutorial
* JMESPath Examples
Examples#
Note
The code node Mode must be set to Run once for each item to test these examples.
JMESPath projections#
JMESPath supports five kinds of projections:
- JMESPath List and Slice projections
- Object Projections
- Flatten Projections
- Filter Projections
Example JSON from a webhook node#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
JMESPath projection#
Retrieve a list of first names from the JSON
The following JavaScript expressions return the following names: * James * Jacob * Jayden
JavaScript
1 |
|
JavaScript for Code node:
1 2 |
|
Python for Code node:
1 2 |
|
Slice projections#
The following projections return the following names: * James * Jacob * Jadyen
1 |
|
JavaScript for code node:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Python code to return two first names: * James * Jacob
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Object projections#
Return a list of dog ages: * 7 * 5
JavaScript expression
1 |
|
Javascript for the Code node
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Python for the Code node
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Multiselect elements#
Use multiselect list to get the first and last names and create new lists containing both names:
* ["James","Green"]
* ["Jacob","Jones"]
* ["Jayden","Smith"]
JavaScript expression
[[% raw %]]
1 |
|
JavaScript for the Code node
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Python for the Code node
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
An alternative to arrow functions in expressions#
For example, generate some input data by returning the below code from the Code node:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
You could do a search like "find the item with the name Lenovo and tell me their category ID."
1 |
|