JSONPath · lesson 13 of 13 · Gotchas

Filter, then index

Selectors chain: each one applies to the children of what the previous one matched. So [?…][0] does not mean "the first match".

Examples

Example 1The matches themselves

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[[email protected] < 10].title
  1. [[email protected] < 10] Matches two book objects; .title then reads each one's title.

Result

"Sayings of the Century"
"Moby Dick"

Try it in the playground

Example 2Trying to take the first match

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[[email protected] < 10][0].title
  1. [[email protected] < 10] Matches the two cheap book objects…
  2. [0] …then looks for index 0 inside each book — an object, which has no index 0 — so nothing is left.

Result

No results.

Try it in the playground

Good to know

  • To get only the first match, take the first item of the result list — in the playground, the first result.

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.