-
Notifications
You must be signed in to change notification settings - Fork 1.6k
For JJ users
pkoppstein edited this page Feb 22, 2023
·
9 revisions
"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."
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 "#".
@this >> .
@this.0 >> .[0]
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")
The equivalent of jq .[] <<< [1,2,3]
is:
jj -l @this <<< [1,2,3]
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 up to and including jq 1.6 translates JSON numbers into IEEE 754 64-bit numbers and thereafter preserves precision of numeric literals, 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
$
- 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
- Home
- FAQ
- jq Language Description
- Cookbook
- Modules
- Parsing Expression Grammars
- Docs for Oniguruma Regular Expressions (RE.txt)
- Advanced Topics
- Guide for Contributors
- How To
- C API
- jq Internals
- Tips
- Development