JSON Path Specification

JSON Path is a query language for JSON documents.

Selectors

Selectors

Description

$

The root object

@

The current object

.<name>

Dot-notated child

[<string>]

Bracket-notated child

[<number>]

Array index

[start:end]

Array slice

[start:end:step]

Extended array slice

[<expression>]

Filter

{<expression>}

Non-descending filter

?

Existence check

Examples

{
    "books": [
        {
            "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
        }
    ],
    "'~'": "value"
}

JSON Path

Result

$.books[@]

All books

$.books[:].author

The authors of all books

$.books[2]

The third book

$.books[-1]

The last book

$.books[:2]

The first two books

$.books[@.isbn]

All books with an ISBN

$.books[:].isbn?

The ISBNs of all books with an ISBN

$.books[@.price < 10]

All books cheaper than 10

$.books[:].price{@ < 10}

The prices of all books cheaper than 10

$.books[@.category == 'fiction' && @.price < 20]

All fiction books cheaper than 20

$['~'~~~'']

The value of the key '~'

Grammar

Generated with RR - Railroad Diagram Generator by Gunther Rademacher.

query

A query can be absolute or relative.

_images/query.svg _images/query1.svg

absolute_query

An absolute query starts with $ followed by zero or more selectors.

absolute_query ::= '$' ( '?'? (
                   '.' identifier
                   | '{' filter '}'
                   | '[' ( slice | integer | string | filter ) ']' )
                   )* '?'?
_images/absolute_query.svg _images/absolute_query1.svg

relative_query

A relative query starts with @ followed by zero or more child selectors.

relative_query ::= '@' ( '.' identifier | '[' ( slice | string | integer ) ']' )*
_images/relative_query.svg _images/relative_query1.svg

filter

A filter consists of one or more (non-)existence checks / comparisons.

_images/filter.svg _images/filter1.svg

value

A value can be a string, number, true, false or null.

value ::= string | number | 'true' | 'false' | 'null'
_images/value.svg _images/value1.svg

slice

A slice has a start and an end index (exclusive) with an optional step.

slice ::= integer? ':' integer? ( ':' integer? )?
_images/slice.svg _images/slice1.svg

string

A string is a sequence of characters, wrapped in single quotes, using tilde escapes.

string ::= "'" ( [^'~] | '~' ['~] )* "'"
_images/string.svg _images/string1.svg

integer

An integer is a signed decimal number.

integer ::= '-'? ( '0' | [1-9] [0-9]* )
_images/integer.svg _images/integer1.svg

number

A number is a signed decimal number, optionally in scientific notation or one of the special values Infinity and -Infinity.

number ::= '-'? (
           ( '0' | [1-9] [0-9]* ) ( '.' [0-9]+ )? ( [eE] [+-]? [0-9]+ )?
           | 'Infinity'
           )
_images/number.svg _images/number1.svg

operator

An operator can be <=, <, ==, !=, >= or >.

operator ::= '<=' | '<' | '==' | '!=' | '>=' | '>'
_images/operator.svg _images/operator1.svg

whitespace

Whitespace can be inserted around operators.

whitespace ::= '#x20'*
_images/whitespace.svg _images/whitespace1.svg