JSON filters and tests reference
The assertion checks use a cut-down version of the Jinja templating library. To use a filter to find an item in a list and extract it as a variable, see JSON: advanced features - filtering a list & returning a value.
Filters available
More information on the filters is available in the Jinja templating library documentation. A selection of useful filters include:
Filter | Description | Parameters |
|---|---|---|
first | Returns the first item in a list | |
join | Joins a list into a string, e.g. list|join(", ") creates a comma separated list | String to join values with |
last | Returns the last item in a list | |
length | Returns the length of a list | |
map | Applies a filter on a sequence of objects or looks up an attribute. This is useful when dealing with lists of objects but you are really only interested in a certain value of it. | attribute - attribute to look up from list objects |
reject | Filters a sequence of objects by applying a test to each object, and rejecting the objects with the test succeeding. | Test for filter (see below) |
rejectattr | Filters a sequence of objects by applying a test to the specified attribute of each object, and rejecting the objects with the test succeeding. | Attribute to look up from list objects |
replace | Return a copy of the value with all occurrences of a substring replaced with a new one. | |
select | Filters a sequence of objects by applying a test to each object, and only selecting the objects with the test succeeding. | Test for filter (see below) |
selectattr | Filters a sequence of objects by applying a test to the specified attribute of each object, and only selecting the objects with the test succeeding. | Attribute to look up from list objects |
sort | Sort an iterable | reverse - reverse sort |
unique | Returns a list of unique items from the given iterable. | case_sensitive - whether sorting is case sensitive |
Tests available
Tests are used as part of the filters. More information on the tests is available in the Jinja templating library documentation. A selection of useful tests include:
Test | Description |
|---|---|
eq, equalto, == | Equal to |
ge, >= | Greater or equal to |
gt, > | Greater than |
le, <= | Less than or equal to |
lt, < | Less than |
ne, != | Not equal to |
none | Returns true if the variable is null |
undefined | Returns true if the variable does not exist |