JSONPathParser Function

Number of input and output field

Number of input field : One
Number of output field : One

The JSONPathParser function is used to read and extract data from a JSON document structure.

JSONPathParser has two parameters:

JSONPath uses the symbol $ to represent the root (outer-level) object.

JSONPath expressions can use the dot notation, for example:

$.store.book[0].title

or square bracket notation, for example:

$['store']['book'][0]['title']

JSONPath allows the wildcard symbol * for member names and array indices.

Expressions (<expr>) can be used as an alternative to names or indices, for example:

$.store.book[(@.length-1)].title

In this case, the symbol '@' refers to the current object.

Filter expressions are supported using the syntax ?(<boolean expr>), for example :

$.store.book[?(@.price < 10)].title

Below is a complete overview and side-by-side comparison of JSONPath syntax elements with their XPath counterparts.

XPath JSONPath Description
/ $ the root object/element
. @ the current object/element
/ . or [] child operator
.. n/a parent operator
// .. recursive descent.
* * wildcard. All objects/elements regardless their names.
@ n/a attribute access. JSON structures don't have attributes.
[] [] subscript operator
| [,] Union operator
n/a [start:end:step] array slice operator .
[] ?() applies a filter (script) expression..
n/a () script expression, using the underlying script engine.
() n/a grouping in Xpath

XPath offers far more capabilities (such as location paths in non-abbreviated syntax, operators, and functions) than those listed here. In addition, there is a significant difference in how the subscript operator works in XPath compared to JSONPath.

In XPath, square brackets always operate on the node set resulting from the previous path fragment, and indices always start at 1.

In JSONPath, square brackets operate on the object or array addressed by the previous path fragment, and indices always start at 0.

Below is a simple JSON structure example representing a bookstore.


  { "store": {
    "book": [ 
      { "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      { "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      { "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      { "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
 }

XPath and JSONPath sample expressions and their results.

XPath JSONPath Result
/store/book/author $.store.book[*].author the authors of all books in the store
//author $..author all authors
/store/* $.store.* all things in store, which are some books and a red bicycle
/store//price $.store..price the price of everything in the store
//book[3] $..book[2] the third book
//book[last()] $..book[(@.length-1)]
$..book[-1:]
the last book in order.
//book[position()<3] $..book[0,1]
$..book[:2]
the first two books
//book[isbn] $..book[?(@.isbn)] filter all books with isbn number
//book[price<10] $..book[?(@.price<10)] filter all books cheapier than 10
//* $..* All Elements in XML document. All members of JSON structure.


To use a field’s content inside a JSONPath expression, use the syntax {field_index}.

For example:
$..book[?(@.price<{1})]

This expression evaluates whether the value of price is less than the content of the second field (field index 1).

Examples of using JSONPathParser in HGrid247.

1. Get all books in store

JSONPath Expresssion$.store.book
Result{{category:reference,author:Nigel Rees,title:Sayings of the Century,price:8.95},{category:fiction,author:Evelyn Waugh,title:Sword of Honour,price:12.99},{category:fiction,author:Herman Melville,title:Moby Dick,isbn:0-553-21311-3,price:8.99},{category:fiction,author:J. R. R. Tolkien,title:The Lord of the Rings,isbn:0-395-19395-8,price:22.99}}

The result of the JSONPath expression is an array of book objects, where each book is represented as:
{ "category": "value1", "author": "value2", ... }

To process this array:

This allows you to extract and work with each field (category, author, etc.) separately.

2. Get all books in the store where the price is lower than 10.

JSONPath Expresssion$.store.book[?(@.price < 10)]
Result{{category:reference,author:Nigel Rees,title:Sayings of the Century,price:8.95},{category:fiction,author:Herman Melville,title:Moby Dick,isbn:0-553-21311-3,price:8.99}}

3. Get titles of all books

JSONPath Expresssion$.store.book[*].title
Result{Sayings of the Century,Sword of Honour,Moby Dick,The Lord of the Rings}

4. Get the titles of all books where the price is lower than 10.

JSONPath Expresssion$.store.book[?(@.price < 10)].title
Result{Sayings of the Century,Moby Dick}

5.Get the title of the first book in the store.

JSONPath Expresssion$.store.book[0].title
ResultSayings of the Century

6.Get all items in the store.

JSONPath Expresssion$.store
Result{book:{{category=reference,author=Nigel Rees,title=Sayings of the Century,price=8.95},{category=fiction,author=Evelyn Waugh,title=Sword of Honour,price=12.99},{category=fiction,author=Herman Melville,title=Moby Dick,isbn=0-553-21311-3,price=8.99},{category=fiction,author=J. R. R. Tolkien,title=The Lord of the Rings,isbn=0-395-19395-8,price=22.99}},bicycle:{color=red,price=19.95}}

Notes: field and value separator for every level

  1. :
  2. =
  3. #
  4. *
  5. $

7.Get all books that have an ISBN.

JSONPath Expresssion$..book[?(@.isbn)]
Result{{category:fiction,author:Herman Melville,title:Moby Dick,isbn:0-553-21311-3,price:8.99},{category:fiction,author:J. R. R. Tolkien,title:The Lord of the Rings,isbn:0-395-19395-8,price:22.99}}

8.Get all books that have no ISBN.

JSONPath Expresssion$..book[?(!@.isbn)]
Result{{category:reference,author:Nigel Rees,title:Sayings of the Century,price:8.95},{category:fiction,author:Evelyn Waugh,title:Sword of Honour,price:12.99}}

9.Get all books whose category is equal to 'reference'.

JSONPath Expresssion$..book[?(@.category=='reference')]
Result{{category:reference,author:Nigel Rees,title:Sayings of the Century,price:8.95}}