JSONPath: Extracting Values from JSON
    • Dark
      Light

    JSONPath: Extracting Values from JSON

    • Dark
      Light

    Article summary

    JSONPath is a query language that allows you to parse and extract information from JSON documents. It is similar in concept to XPath, which is used to extract data from XML documents. In JSONPath, the dot (.) character is used to select elements from the JSON structure.

    Syntax

    JSONPath expressions always refer to a JSON structure in the same way as XPath expression are used in combination with an XML document. The root object is referred to by the dollar symbol ($), and child elements are accessed via dot notation.

    A basic template for writing JSONPath syntax would look something like this:

    $.store.book[0].title

    This JSONPath expression would be used to access the title of the first book in the store.

    Operators

    • $: The root object/element.
    • @: The current object/element.
    • . or []: Child operator.
    • ..: Recursive descent.
    • *: Wildcard. All objects/elements regardless of their names.
    • [,]: Union operator in XPath results in a combination of items.
    • [start:end:step]: Array slice operator borrowed from ES2015/Python.

    Examples

    Consider the following JSON document:

    {
        "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
                }
            ],
            "bicycle": {
                "color": "red",
                "price": 19.95
            }
        },
        "expensive": 10
    }
    

    Here are some JSONPath expressions and their corresponding results based on the given JSON document:

    • $.store.book[*].author: This JSONPath expression selects all authors of all books in the store. The result is an array of authors' names:
    ["Nigel Rees", "Evelyn Waugh", "Herman Melville"]
    • $.store.*: This JSONPath expression selects all items inside store. The result is an array of the book array and the bicycle object:
      [
          [
              {
                  "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
              }
          ],
          {
              "color": "red",
              "price": 19.95
          }
      ]
      
    • $.store..price: This JSONPath expression selects all prices in the store, either for a book or the bicycle. The result is an array of all prices:
    [8.95, 12.99, 8.99, 19.95]
    
    • $..book[2]: This JSONPath expression selects the third book in the book array. The result is a single book object:
    {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
    }
    
    • $.store.book[?(@.price < 10)]: This JSONPath expression selects all books in the store that cost less than 10. The result is an array of the books that match this criterion:
      [
          {
              "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
          }
      ]
      With these examples, it's clear to see how JSONPath can be used to extract a wide variety of data from a JSON document, either by selecting specific properties or by using various operators to select a range of items.