JSONPath · lesson 2 of 13 · Basics

Array indexes

[n] picks one element (zero-based). A negative index counts from the end.

Examples

Example 1The first book

Document

{
  "store": {
    "book": [
      { "title": "Sayings of the Century", "author": "Nigel Rees",
        "category": "reference", "price": 8.95 },
      { "title": "Sword of Honour", "author": "Evelyn Waugh",
        "category": "fiction", "price": 12.99 },
      { "title": "Moby Dick", "author": "Herman Melville",
        "category": "fiction", "price": 8.99, "isbn": "0-553-21311-3" },
      { "title": "The Lord of the Rings", "author": "J. R. R. Tolkien",
        "category": "fiction", "price": 22.99, "isbn": "0-395-19395-8" }
    ],
    "bicycle": { "color": "red", "price": 19.95 }
  }
}

Query

$.store.book[0].title
  1. .book The array of books.
  2. [0] Index 0: the first element.
  3. .title Its title.

Result

"Sayings of the Century"

Try it in the playground

Example 2The last book

Document

{
  "store": {
    "book": [
      { "title": "Sayings of the Century", "author": "Nigel Rees",
        "category": "reference", "price": 8.95 },
      { "title": "Sword of Honour", "author": "Evelyn Waugh",
        "category": "fiction", "price": 12.99 },
      { "title": "Moby Dick", "author": "Herman Melville",
        "category": "fiction", "price": 8.99, "isbn": "0-553-21311-3" },
      { "title": "The Lord of the Rings", "author": "J. R. R. Tolkien",
        "category": "fiction", "price": 22.99, "isbn": "0-395-19395-8" }
    ],
    "bicycle": { "color": "red", "price": 19.95 }
  }
}

Query

$.store.book[-1].title
  1. [-1] A negative index counts from the end: -1 is the last element.

Result

"The Lord of the Rings"

Try it in the playground

About jsonquery

Query, format and learn JSON with jq, JSONPath, JMESPath and JSON Pointer. It is the engine of the desktop app, run in your browser.

Engine
The desktop app’s, compiled to WebAssembly
Licence
MIT (the engine and the app)
Source
GitHub
Contact
The contact form (private)
Public issues
GitHub
Legal
Privacy policy

Known limitations: a file over 10 MB opens read-only and one over 512 MB does not open here; the desktop app has no such limit.