JSONPath Expression

  • Last update: August 23, 2024
  • Overview

    JSONPath expressions can be used to parse JSON-structured data. For details, see the official API document: https://github.com/json-path/JsonPath.

    Basic Operator

    The root element in JSONPath is always referred to as $ regardless if it is an object or array.

    JSONPath expressions allow you to use the dot notation like $.store.book[0].title or the square bracket notation like $['store']['book'][0]['title'].

    Operator
    Description
    $

    It is the root element.

    . or []

    To obtain the child element.

    Example: store.book or store["book"]

    ..

    It is the descendant selector for selecting all elements that meet the condition.

    [,]

    It is the coalescing operator for selecting multiple elements.

    Example: book [0,1] or $.. book [0].["category,""author"]

    [start:end]

    It is the slicing operator, with the index starting from 0, and a left-closed, right-open range.

    Example: book [0:2] or book [-2:0]

    ?()

    It is the filter identifier for selecting elements in an array, where the filtering condition can be written within parentheses.

    @

    It represents the element currently being processed by the filter, usually used with the filter.

    Example: book [?(@. price <10)]

    *

    It is the wildcard for matching the element name or index.

    Example: book.* or book[*]

    It can also be used with the filter as part of a regular expression.

    Example: book[?(@.author =~ /.*ees/)]

    Filter Operator

    Filters are logical expressions used to filter arrays. A typical filter is [?(@.age > 18)] where @ represents the current element being processed. You can create more complex filters with logical operators && and ||. The value of the string must be enclosed by single or double quotes, such as ([?(@.color == 'blue')] or [?(@.color == "blue")]).

    OperatorDescription

    ==

    Left is equal to right.
    iconNote:
    Data types will be compared. For example, 1 and '1' are not equal.

    !=

    Left is not equal to right.

    <

    Left is less than right.

    <=

    Left is less than or equal to right.

    >

    Left is greater than right.

    >=

    Left is greater than or equal to right.

    =~

    Left matches the regular expression of right.

    Example: [?(@. name =~/foo. *?/i)]

    in

    Left exists in right.

    Example: [?(@.size in ['S', 'M'])]

    nin

    Left does not exists in right.

    subsetof

    Left is a subset of right.

    Example: [?(@. sizes subsetof ['S', 'M', 'L'])]

    anyof

    Left has an intersection with right.

    Example: [?(@.sizes anyof ['M', 'L'])]

    noneof

    Left has no intersection with right.

    Example: [?(@.sizes noneof ['M', 'L'])]

    size

    Size of left (array/string) should match right.

    empty

    Left (array/string) should be empty.

    Function

    You can invoke a function at the end of a path.

    FunctionDescriptionOutput Type

    max()

    Provides the maximum value of an array of numbers.

    Double precision

    avg()

    Provides the average value of an array of numbers.

    Double precision

    stddev()

    Provides the standard deviation value of an array of numbers.

    Double precision

    length()

    Provides the length of an array.

    Integer

    sum()

    Provides the sum value of an array of numbers.

    Double precision

    Example:

    Here is a JSON structure.

    {
        "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
            }
        },
        "expensive": 10
    }
    JSONPath ExpressionJSONPath Expression
    $.store.book[*].author

    The authors of all books in the store element

    $..author

    All authors in the root element

    $.store.*

    Everything in the store element, whether it is a book or a bicycle

    $.store..price

    The price of everything in the store element

    $..book[2]

    The third book

    $..book[-2]

    The second to last book

    $..book[0,1]

    $..book[:2]

    The first two books

    $..book[:2]

    The first two books (Note: The index of the first book is 0.)

    $..book[1:2]

    The second book

    $..book[-2:]

    Last two books

    $..book[2:]

    The third to the last book

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

    All books with an ISBN number

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

    All books cheaper than 10

    $..book[?(@.price <= $['expensive'])]

    All books where the price does not exceed the value of expensive (The value of expensive is 10.)

    $..book[?(@.author =~ /.*REES/i)]

    All books matching the regular expression

    Meaning of the regular expression: The author's name ends with REES, which is not case-sensitive.

    $..*

    Everything in the root element

    $.store.book.length()

    The total number of books

     

    附件列表


    主题: Data Development - Scheduled Task
    Previous
    Next
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy