JMESPath · lesson 7 of 15 · Filtering
Filters & literals
[?condition] keeps the elements for which the condition is true. Numbers, booleans and null are written as JSON literals in backticks — `30` — while plain strings can use single quotes: 'dev'.
Examples
Example 1Compare with a number
Document
{
"team": "Platform",
"office": { "city": "Berlin", "floor": 3 },
"members": [
{ "name": "Ada", "age": 36, "role": "lead", "active": true,
"email": "[email protected]", "skills": ["rust", "sql"] },
{ "name": "Linus", "age": 28, "role": "dev", "active": true,
"email": null, "skills": ["c", "shell"] },
{ "name": "Grace", "age": 45, "role": "dev", "active": false,
"skills": ["cobol"] },
{ "name": "Alan", "age": 41, "role": "qa", "active": true,
"email": "[email protected]", "skills": [] }
]
}Query
members[?age > `30`].name
[?Start of a filter: keep the elements for which the test is true.age > `30`Comparesagewith`30`— a number literal, which must be written in backticks..nameThen project their names.
Result
["Ada","Grace","Alan"]
Example 2Compare with a string
Document
{
"team": "Platform",
"office": { "city": "Berlin", "floor": 3 },
"members": [
{ "name": "Ada", "age": 36, "role": "lead", "active": true,
"email": "[email protected]", "skills": ["rust", "sql"] },
{ "name": "Linus", "age": 28, "role": "dev", "active": true,
"email": null, "skills": ["c", "shell"] },
{ "name": "Grace", "age": 45, "role": "dev", "active": false,
"skills": ["cobol"] },
{ "name": "Alan", "age": 41, "role": "qa", "active": true,
"email": "[email protected]", "skills": [] }
]
}Query
members[?role == 'dev'].name'dev'A raw string literal in single quotes. The JSON literal`"dev"`works too.
Result
["Linus","Grace"]
Good to know
- The comparison operators are
==,!=,<,<=,>and>=. - Decimal literals such as
`9.5`don't evaluate correctly in this build yet; whole numbers, strings, booleans andnullare fine.