JavaScript

json_path Method

Syntax

result = json_path(json, path)

Arguments

jsonstringJSON Object

The JSON definition, defined as a string or JSON Object.

pathstring

The path defining what portion of the JSON to extract.

Returns

resultJSON Object

Returns the portion of the JSON that matches the path.

Description

Extracts a portion of a JSON string or object.

Discussion

The json_path() method allows you to extract portions of a JSON string and perform certain types of simple calculations on a JSON string.

The json_path() method is only available in the UX component. In addition, you must explicitly load the jsonpath system library by selecting it at the Optional system libraries property in the UX Builder if you want to use the json_path() method in your code.

The example below extracts the "book" array from the JSON object:

var path = '$.store.book[*]';

var json = {
  "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
    }
  }
}

var result = json_path(json,path);

alert(JSON.stringify(result));

The chart below describes what will be returned by the json_path() method for the specified path. The examples in this chart are from the NPM JSONPath documentation:

JSON Path Syntax

Returns

$.store.book[*].author

The authors of all books in the store

$..author

All authors

$.store.*

All things in store, which are its books (a book array) and a red bicycle (a bicycle object).

$.store..price

The price of everything in the store.

$..book[2]

The third book (book object)

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

The last book in order.

$..book[-1:]

$..book[0,1]

The first two books

$..book[:2]

$..book[0][category,author]

The categories and authors of all books

$..book[?(@.isbn)]

Filter all books with an ISBN number

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

Filter all books cheaper than 10

$..*[?(@property === 'price' && @ !== 8.95)]

Obtain all property values of objects whose property is price and which does not equal 8.95

$

The root of the JSON object (i.e., the whole object itself)

$..*

All Elements (and text) beneath root in an XML document. All members of a JSON structure beneath the root.

$..

All Elements in an XML document. All parent components of a JSON structure including root.

$..[?(@.price>19)]^

Parent of those specific items with a price greater than 19 (i.e., the store value as the parent of the bicycle and the book array as parent of an individual book)

$.store.*~

The property names of the store sub-object ("book" and "bicycle"). Useful with wildcard properties.

$.store.book[?(@path !== "$['store']['book'][0]")]

All books besides that at the path pointing to the first

$..book[?(@parent.bicycle && @parent.bicycle.color === "red")].category

Grabs all categories of books where the parent object of the book has a bicycle child whose color is red (i.e., all the books)

$..book.*[?(@property !== "category")]

Grabs all children of "book" except for "category" ones

$..book[?(@property !== 0)]

Grabs all books whose property (which, being that we are reaching inside an array, is the numeric index) is not 0

$.store.*[?(@parentProperty !== "book")]

Grabs the grandchildren of store whose parent property is not book (i.e., bicycle's children, "color" and "price")

$..book.*[?(@parentProperty !== 0)]

Get the property values of all book instances whereby the parent property of these values (i.e., the array index holding the book item parent object) is not 0

$..book..*@number()

Get the numeric values within the book array

json_path in Xbasic 

You can also use JSON Path in server-side scripts (in your Xbasic code). The Xbasic function is also called json_path().

See Also