For JJ users - troyp/jq GitHub Wiki

"JJ is a command line utility that provides a fast and simple way to retrieve or update values from JSON documents. ... It's fast because it avoids parsing irrelevant sections of json, skipping over values that do not apply, and aborts as soon as the target value has been found or updated."

Path Syntax

JJ uses a "Path Syntax" that has some similarity to jq but there are significant differences. Here are some JJ queries together with equivalent jq expressions, assuming the input is a single JSON object. In some cases, additional translations are shown or indicated following a "#".

name.last          >>  .name.last
age                >>  .age
children           >>  .children
children.#         >>  .children|length
children.1         >>  .children[1]
child*.2           >>  to_entries[] | select(.key|test("^child")) | .value[2]
c?ildren.0         >>  to_entries[] | select(.key|test("^c.ildren$")) | .value[0]
fav\.movie         >>  .["fav.movie"]  # alternatively: ."fav.movie"
friends.#.first    >>  [.friends[].first] .  # or: .friends | map(.first)
friends.1.last     >>  .friends[1].last
friends.#[last=="Murphy"].first    >> .friends[] | first(select(.last=="Murphy")) | .first
friends.#[last=="Murphy"]#.first   >> .friends[] | select(.last=="Murphy") | .first
friends.#[age>45]#.last            >> .friends[] | select(.age > 45) | .last
friends.#[first%"D*"].last         >> .friends[] | first(select(.first|startswith("D"))) | .last  # or test("^D")

Semantic Differences [PRELIMINARY]

The following listing is probably incomplete.

  • Repeated Keys - If a key is repeated within a single object, JJ retrieves the first corresponding value whereas the regular jq parser ignores all but the last; however, the streaming jq parser can be used to ascertain all the given values.
  • Numbers - jq translates JSON numbers into IEEE 754 64-bit numbers, thus ensuring consistency, whereas JJ generally retains the numerical representation of numbers, but is sometimes inconsistent, e.g.
    $ jj a -p <<< '{"a": 1e2000}'
    null
    $ jj a -p <<< 1e2000
     
    $

Some Other Differences

  • Unless specifically instructed to do otherwise, jq will only output a stream of valid JSON entities, whereas JJ sometimes emits not-strictly-valid numbers, e.g.
    $ jj -p <<< 00000
    00000