Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In this Page

...

SyntaxExample PathResultDescription
 $ $
The document root.
 . (dot) $.parent.childThe 'child' field from the 'parent' object.Child operator. This operator is used to select a field in a parent object.
 [] $.parent['child with spaces']The 'child with spaces' field from the 'parent' object.Child operator or array index. This operator can used to select a field that may contain special characters that need to be quoted.

 $.children[1]The second element of the 'child' array.

 $.children[-1]The last element in the array.
 * $.children[*].ageThe 'age' values from the elements of the 'children' array.Wildcard operator. Selects all elements in an array or an object.
 .. $..childAll of 'child' fields found in the document.The descent operator. Traverses the entire document searching for the fields and elements that match the rest of the path.
 [start:end:step] $.children[1:]All but the first element of the 'children' array.The array slice operator. The 'start' value specifies the starting index for the slice. If not specified, then zero is used for the start. The 'end' value specifies the exclusive end of the slice. If no end is given, then the end of the list is used. The 'step' value specifies how many elements the operator moves through at a time. If no step is given, then one is used. If the 'start' or 'end' values fall outside of the bounds of the array, they will automatically be adjusted to the nearest bounds and not trigger an error.

 $.children[::2]Every other element in the 'children' array.

 $.children[::-1]All elements of the 'children' array in reverse order.
 [,] $.parent['child1','child2']The fields of the 'parent' object named 'child1' and 'child2'.The union operator. Selects the array elements or object fields with the given indexes or names, respectively.
 ?(expr) $.children[?(value.age > 18)]Only those elements in the 'children' array where the 'age' field is greater than 18.The filter operator. Selects all elements in an array or an object that match the given expression. In the expression, the 'value' variable refers to the array element or object value and the 'key' variable refers to the field name in the object.
 (expr) $.parent[(_name)]The field from the 'parent' object that is specified by the 'name' pipeline parameter.The expression operator. Select the array element or object field as specified by the enclosed expression.
{defaultValue:<value>}($, '$.store.book.author*',
{defaultValue: Anonymous}
)
When the book author is not found, the author is listed as Anonymous.Sets a default value for JSONPath. If a specified path exists, it's value is returned. If any part of the path is missing, then the default value is returned.  

Extended Syntax

When using JSONPath to query a document, the basic syntax can cover the majority of use-cases, however, there are often times when you wish to process the results of a query. For example, if an array contained a list of addresses (e.g. home, work), you could use a filter to select the home address (i.e. $.addresses[?(value.type == 'home')]), but the result is still in a list. So, rather than require an extra Snap to extract the address object from a list, we have extended the standard JSONPath syntax with some method calls that can be used to manipulate the results of a query.

...

  • jsonPath($, "$user[*].screenName")
  • jsonPath($, "$user[*]['full name']")

...

See Also

...