JSONPath · lesson 12 of 13 · Filters

Functions

RFC 9535 defines length(), count(), match(), search() and value() for use inside filters.

Examples

Example 1Long titles

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[?length(@.title) > 15].title
  1. length(@.title) The number of characters in the title.
  2. > 15 Keep books whose title is longer than 15 characters.

Result

"Sayings of the Century"
"The Lord of the Rings"

Try it in the playground

Example 2Authors matching a pattern

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[?match(@.author, "^[NE].*")].title
  1. match( A regular-expression test that must match the whole string.
  2. "^[NE].*" Names starting with N or E, followed by anything.

Result

"Sayings of the Century"
"Sword of Honour"

Try it in the playground

Good to know

  • search(@.title, "the") matches anywhere inside the string; match needs the whole string to match.
  • count(@.*) counts how many members an element has: [?count(@.*) > 4] finds the books that carry an ISBN.
  • Functions only work inside [? … ]: $.store.book.length() is a syntax error.

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.