3 JSONPath functionality

Overview

This section provides details of supported JSONPath functionality in item value preprocessing steps.

JSONPath consists of segments separated with dots. A segment can be either a simple word like a JSON value name, * or a more complex construct enclosed within square brackets [ ]. The separating dot before bracket segment is optional and can be omitted. For example:

PathDescription
$.object.nameReturn the object.name contents.
$.object[‘name’]Return the object.name contents.
$.object.[‘name’]Return the object.name contents.
$[“object”][‘name’]Return the object.name contents.
$.[‘object’].[“name”]Return the object.name contents.
$.object.history.length()Return the number of object.history array elements.
$[?(@.name == ‘Object’)].price.first()Return the price field of the first object with name ‘Object’.
$[?(@.name == ‘Object’)].history.first().length()Return the number of history array elements of the first object with name ‘Object’.
$[?(@.price > 10)].length()Return the number of objects with price being greater than 10.

See also: Escaping special characters from LLD macro values in JSONPath.

Supported segments

SegmentDescription
<name>Match object property by name.
Match all object properties.
[‘<name>’]Match object property by name.
[‘<name>’, ‘<name>’, …]Match object property by any of the listed names.
[<index>]Match array element by the index.
[<number>, <number>, …]Match array element by any of the listed indexes.
[]Match all object properties or array elements.
[<start>:<end>]Match array elements by the defined range:
<start> - the first index to match (including). If not specified matches all array elements from the beginning. If negative specifies starting offset from the end of array.
<end> - the last index to match (excluding). If not specified matches all array elements to the end. If negative specifies starting offset from the end of array.
[?(<expression>)]Match objects/array elements by applying filter expression.

To find a matching segment ignoring its ancestry (detached segment) it must be prefixed with ‘..’ , for example $..name or $..['name'] return values of all ‘name’ properties.

Matched element names can be extracted by adding a ~ suffix to the JSONPath. It returns the name of the matched object or an index in string format of the matched array item. The output format follows the same rules as other JSONPath queries - definite path results are returned ‘as is’ and indefinite path results are returned in array. However there is not much point of extracting the name of an element matching a definite path - it’s already known.

Filter expression

Filter expression is a arithmetical expression in infix notation.

Supported operands:

OperandDescriptionExample
“<text>”
‘<text>’
Text constant.‘value: \’1\’’
“value: ‘1’”
<number>Numeric constant supporting scientific notation.123
<jsonpath starting with $>Value referred to by the JSONPath from the input document root node; only definite paths are supported.$.object.name
<jsonpath starting with @>Value referred to by the JSONPath from the current object/element; only definite paths are supported.@.name

Supported operators:

OperatorTypeDescriptionResult
-binarySubtraction.Number.
+binaryAddition.Number.
/binaryDivision.Number.
*binaryMultiplication.Number.
==binaryIs equal to.Boolean (1 or 0).
!=binaryIs not equal to.Boolean (1 or 0).
<binaryIs less than.Boolean (1 or 0).
<=binaryIs less than or equal to.Boolean (1 or 0).
>binaryIs greater than.Boolean (1 or 0).
>=binaryIs greater than or equal to.Boolean (1 or 0).
=~binaryMatches regular expression.Boolean (1 or 0).
!unaryBoolean not.Boolean (1 or 0).
||binaryBoolean or.Boolean (1 or 0).
&&binaryBoolean and.Boolean (1 or 0).

Functions

Functions can be used at the end of JSONPath. Multiple functions can be chained if the preceding function returns value that is accepted by the following function.

Supported functions:

FunctionDescriptionInputOutput
avgAverage value of numbers in input array.Array of numbers.Number.
minMinimum value of numbers in input array.Array of numbers.Number.
maxMaximum value of numbers in input array.Array of numbers.Number.
sumSum of numbers in input array.Array of numbers.Number.
lengthNumber of elements in input array.Array.Number.
firstThe first array element.Array.A JSON construct (object, array, value) depending on input array contents.

Quoted numeric values are accepted by the JSONPath aggregate functions. It means that the values are converted from string type to numeric if aggregation is required.

Incompatible input will cause the function to generate error.

Output value

JSONPaths can be divided in definite and indefinite paths. A definite path can return only null or a single match. An indefinite path can return multiple matches, basically JSONPaths with detached, multiple name/index list, array slice or expression segments. However, when a function is used the JSONPath becomes definite, as functions always output single value.

A definite path returns the object/array/value it’s referencing, while indefinite path returns an array of the matched objects/arrays/values.

Whitespace

Whitespace (space, tab characters) can be freely used in bracket notation segments and expressions, for example, $[ 'a' ][ 0 ][ ?( $.b == 'c' ) ][ : -1 ].first( ).

Strings

Strings should be enclosed with single ‘ or double “ quotes. Inside the strings, single or double quotes (depending on which are used to enclose it) and backslashes \ are escaped with the backslash \ character.

Examples

Input data
  1. {
  2. "books": [
  3. {
  4. "category": "reference",
  5. "author": "Nigel Rees",
  6. "title": "Sayings of the Century",
  7. "price": 8.95,
  8. "id": 1
  9. },
  10. {
  11. "category": "fiction",
  12. "author": "Evelyn Waugh",
  13. "title": "Sword of Honour",
  14. "price": 12.99,
  15. "id": 2
  16. },
  17. {
  18. "category": "fiction",
  19. "author": "Herman Melville",
  20. "title": "Moby Dick",
  21. "isbn": "0-553-21311-3",
  22. "price": 8.99,
  23. "id": 3
  24. },
  25. {
  26. "category": "fiction",
  27. "author": "J. R. R. Tolkien",
  28. "title": "The Lord of the Rings",
  29. "isbn": "0-395-19395-8",
  30. "price": 22.99,
  31. "id": 4
  32. }
  33. ],
  34. "services": {
  35. "delivery": {
  36. "servicegroup": 1000,
  37. "description": "Next day delivery in local town",
  38. "active": true,
  39. "price": 5
  40. },
  41. "bookbinding": {
  42. "servicegroup": 1001,
  43. "description": "Printing and assembling book in A5 format",
  44. "active": true,
  45. "price": 154.99
  46. },
  47. "restoration": {
  48. "servicegroup": 1002,
  49. "description": "Various restoration methods",
  50. "active": false,
  51. "methods": [
  52. {
  53. "description": "Checmical cleaning",
  54. "price": 46
  55. },
  56. {
  57. "description": "Pressing pages damaged by moisture",
  58. "price": 24.5
  59. },
  60. {
  61. "description": "Rebinding torn book",
  62. "price": 99.49
  63. }
  64. ]
  65. }
  66. },
  67. "filters": {
  68. "price": 10,
  69. "category": "fiction",
  70. "no filters": "no \"filters\""
  71. },
  72. "closed message": "Store is closed",
  73. "tags": [
  74. "a",
  75. "b",
  76. "c",
  77. "d",
  78. "e"
  79. ]
  80. }
JSONPathTypeResultComments
$.filters.pricedefinite10
$.filters.categorydefinitefiction
$.filters[‘no filters’]definiteno “filters”
$.filtersdefinite{
“price”: 10,
“category”: “fiction”,
“no filters”: “no \”filters\””
}
$.books[1].titledefiniteSword of Honour
$.books[-1].authordefiniteJ. R. R. Tolkien
$.books.length()definite4
$.tags[:]indefinite[“a”, “b”, “c”, “d”, “e” ]
$.tags[2:]indefinite[“c”, “d”, “e” ]
$.tags[:3]indefinite[“a”, “b”, “c”]
$.tags[1:4]indefinite[“b”, “c”, “d”]
$.tags[-2:]indefinite[“d”, “e”]
$.tags[:-3]indefinite[“a”, “b”]
$.tags[:-3].length()definite2
$.books[0, 2].titleindefinite[“Sayings of the Century”, “Moby Dick”]
$.books[1][‘author’, “title”]indefinite[“Evelyn Waugh”, “Sword of Honour”]
$..idindefinite[1, 2, 3, 4]
$.services..priceindefinite[5, 154.99, 46, 24.5, 99.49]
$.books[?(@.id == 4 - 0.4 5)].titleindefinite[“Sword of Honour”]This query shows that arithmetical operations can be used in queries. Of course this query can be simplified to $.books[?(@.id == 2)].title
$.books[?(@.id == 2 || @.id == 4)].titleindefinite[“Sword of Honour”, “The Lord of the Rings”]
$.books[?(!(@.id == 2))].titleindefinite[“Sayings of the Century”, “Moby Dick”, “The Lord of the Rings”]
$.books[?(@.id != 2)].titleindefinite[“Sayings of the Century”, “Moby Dick”, “The Lord of the Rings”]
$.books[?(@.title =~ “ of “)].titleindefinite[“Sayings of the Century”, “Sword of Honour”, “The Lord of the Rings”]
$.books[?(@.price > 12.99)].titleindefinite[“The Lord of the Rings”]
$.books[?(@.author > “Herman Melville”)].titleindefinite[“Sayings of the Century”, “The Lord of the Rings”]
$.books[?(@.price > $.filters.price)].titleindefinite[“Sword of Honour”, “The Lord of the Rings”]
$.books[?(@.category == $.filters.category)].titleindefinite[“Sword of Honour”,”Moby Dick”,”The Lord of the Rings”]
$..[?(@.id)]indefinite[
{
“category”: “reference”,
“author”: “Nigel Rees”,
“title”: “Sayings of the Century”,
“price”: 8.95,
“id”: 1
},
{
“category”: “fiction”,
“author”: “Evelyn Waugh”,
“title”: “Sword of Honour”,
“price”: 12.99,
“id”: 2
},
{
“category”: “fiction”,
“author”: “Herman Melville”,
“title”: “Moby Dick”,
“isbn”: “0-553-21311-3”,
“price”: 8.99,
“id”: 3
},
{
“category”: “fiction”,
“author”: “J. R. R. Tolkien”,
“title”: “The Lord of the Rings”,
“isbn”: “0-395-19395-8”,
“price”: 22.99,
“id”: 4
}
]
$.services..[?(@.price > 50)].descriptionindefinite‘[“Printing and assembling book in A5 format”, “Rebinding torn book”]
$..id.length()definite4
$.books[?(@.id == 2)].title.first()definiteSword of Honour
$..tags.first().length()definite5$..tags is indefinite path, so it returns an array of matched elements - [[“a”, “b”, “c”, “d”, “e” ]], first() returns the first element - [“a”, “b”, “c”, “d”, “e” ] and finally length() calculates its length - 5.
$.books[].price.min()definite8.95
$..price.max()definite154.99
$.books[?(@.category == “fiction”)].price.avg()definite14.99
$.books[?(@.category == $.filters.xyz)].titleindefiniteA query without match returns NULL for definite and indefinite paths.
$.services[?(@.active==”true”)].servicegroupindefinite[1000,1001]Text constants must be used in boolean value comparisons.
$.services[?(@.active==”false”)].servicegroupindefinite[1002]Text constants must be used in boolean value comparisons.
$.services[?(@.servicegroup==”1002”)]~.first()definiterestoration