JMESPath · lesson 9 of 15 · Filtering
Functions in filters
Any function can appear in a condition, which is how you test text and arrays.
Examples
Example 1Does an array contain a value?
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[?contains(skills, 'sql')].namecontains(skills, 'sql')True when the arrayskillsincludes the string'sql'(on a string,containslooks for a substring).
Result
["Ada"]
Example 2How many items?
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[?length(skills) > `1`].name
length(skills)The number of elements inskills.> `1`…compared with the number 1: members with two or more skills.
Result
["Ada","Linus"]
Good to know
- Functions are strict about types:
ends_with(email, '.com')raises an error for Linus, whose e-mail isnull. Guard it withtype(email) == 'string' && ends_with(email, '.com').