JSONPath

Qubes 6.6 Extracts a sub-set of data from a string in JSON format.

Syntax

JSONPath(string, path)
string = JSON string you want to interpret
path = query specifying the sub-set you want to extract

Remarks

This function systematically returns a list of values corresponding to the path.

A path is a series of relatively-interpreted selectors:

  • .prop selects the immediate sub-element named prop
  • ..prop recursively selects the sub-element named prop
  • [ ] selects a sub-element by its index, starting from the beginning (0) if the index is positive, or from the end if negative

The wildcard * can be used in place of a sub-element's name:

  • .* selects all of the immediate sub-elements
  • ..* recursively selects all of the sub-elements
  • [*] or [] selects all of the table's sub-elements

If the sub-element name contains any non-alphanumeric characters, you can use quotation marks:

  • ."name with special characters"
  • .."name with special characters"
  • ."property with ""escaped"" inner double quotes"

If the sub-element is missing (the property is not declared on every element), it will not be added to the result list. So be cautious when using indexes to retrieve the associated value in the result of JSONPath as the result length will not always be the same as the input length.

Examples

With the following JSON:

{
    "books": [
        {
            "id"     : 1,
            "title"  : "Clean Code",
            "author" : { "name" : "Robert C. Martin" },
            "price"  : 17.96
        },
        {
            "id"     : 2,
            "title"  : "Maintainable JavaScript",
            "author" : { "name" : "Nicholas C. Zakas" },
            "price"  : 10
        },
        {
            "id"     : 3,
            "title"  : "JavaScript: The Good Parts",
            "author" : { "name" : "Douglas Crockford" },
            "price"  : 15.67
        }
    ]
}

you will get the following extractions:

  • To find the authors' names:
    JSONPath(books, '.books.author.name')
    { "Robert C. Martin", "Nicholas C. Zakas", "Robert C. Martin", "Douglas Crockford" }
  • To find the price of the last book:
    JSONPath(books, '.books[-1].price')
    { 15.67 }

Classification

Strings Transcoding