#json-path #json #cli #command-line-tool

app jpq

A JSONPath command line tool to extract values from a JSON value

2 releases

new 0.1.2 Jan 15, 2025
0.1.0 Jan 13, 2025

#308 in Command line utilities

Download history 189/week @ 2025-01-12

189 downloads per month

MIT license

8KB
56 lines

jpq

A JSONPath command line tool to extract values from a JSON value.

Usage

jpq <xpath> <filename>

The xpath argument is a RFC 9535 JSONPath expression. The extracted result is printed on standard output.

Overview of JSONPath Syntax

This is copied from RFC 9535 JSONPath: Query Expressions for JSON

  • $: root node identifier
  • @: current node identifier (valid only within filter selectors)
  • [<selectors>]: child segment: selects zero or more children of a node
  • .name: shorthand for ['name']
  • .*: shorthand for [*]
  • ..[<selectors>]: descendant segment: selects zero or more descendants of a node
  • ..name: shorthand for ..['name']
  • ..*: shorthand for ..[*]
  • 'name': name selector: selects a named child of an object
  • *: wildcard selector: selects all children of a node
  • 3: index selector: selects an indexed child of an array (from 0)
  • 0:100:5: array slice selector: start:end:step for arrays
  • ?<logical-expr>: filter selector: selects particular children using a logical expression
  • length(@.foo): function extension: invokes a function in a filter expression

Example

The authors of all books in the store:

$ jpq '$.store.book[*].author' test-data/rfc9535-fig1.json
[
  "Nigel Rees",
  "Evelyn Waugh",
  "Herman Melville",
  "J. R. R. Tolkien"
]

all books cheaper than 10:

$ target/debug/jpq '$..book[?@.price<10]' test-data/rfc9535-fig1.json
[
  {
    "author": "Nigel Rees",
    "category": "reference",
    "price": 8.95,
    "title": "Sayings of the Century"
  },
  {
    "author": "Herman Melville",
    "category": "fiction",
    "isbn": "0-553-21311-3",
    "price": 8.99,
    "title": "Moby Dick"
  }
]

Changes

0.1.2

  • use miette for user friendly error messages.

0.1.1

  • Change underlying JSONPath library to serde_json_xpath for better support of RFC 9535,

Dependencies

~6–13MB
~157K SLoC