Query
Array elements can be queried by using square brackets ( [ ] ). The query is evaluated against all of the array elements. The query can select any fields of the element for comparison and reference anything in the JSON document.
The following table shows query operators. a and b can be
either a constant or a JPath construct. Basic selection, query, arithmetic, and functions are JPath
constructs.
Operator | Description |
---|---|
a = b | Equal |
a != b | Not equal |
a > b | Greater than |
a < b | Less than |
a >= b | Greater than or equal |
a <= b | Less than or equal |
not a | Negates the result of a |
exists a | Checks if a exists as an attribute |
The following table shows examples of the query operators that you can apply to the array elements.
Example | Description | State | Expression | Result |
---|---|---|---|---|
Equality (or Inequality) | Queries an array for objects with an attribute equal to a value. | { "array": [ { "id": 1, "name": "Object 1" },
{ "id": 2, "name": "Object 2" }, { "id": 3,
"name": "Object 3" } ] } |
/array[@id = 2] |
[ { "id": 2, "name": "Object 2" } ] |
Greater than | Queries an array of objects with attributes greater than a value. | { "array": [ { "id": 1, "name": "Object 1" },
{ "id": 2, "name": "Object 2" }, { "id": 3,
"name": "Object 3" } ] } |
/array[@id > 1] |
[ { "id": 2, "name": "Object 2" }, { "id": 3,
"name": "Object 3" } ] |
Primitives | Selects primitives from an array that passes a specific query. | { "array": [ "value 1", "value 2", "value 3" ]
} |
/array[@ != "value 2"] |
[ "value 1", "value 3" ] |
And | Selects with the 'and' operator. | { "array": [ "value 1", "value 2", "value 3" ]
} |
/array[@ != "value 2" and @ != 'value 3'] |
[ "value 1" ] |
Or | Selects with the 'or' operator. | { "array": [ "value 1", "value 2", "value 3" ]
} |
/array[@ = "value 2" or @ = "value 3"] |
[ "value 2", "value 3" ] |
Parentheses | Selects with parentheses. | { "array": [ "value 1", "value 2", "value 3" ]
} |
/array[not (@ = "value 2" or @ = "value 3")] |
[ "value 1" ] |
Exists | Selects objects of an array that have a specific attribute. | { "array": [ { "id": 1, "name": "Object 1" },
{ "id": 2, "name": "Object 2" }, { "id": 3, } ]
} |
/array[exists @name] |
[ { "id": 1, "name": "Object 1" }, { "id": 2,
"name": "Object 2" } ] |