First release of jq in 5 years
github.com
github.com
I’m excited about pick()! Could have used this so many times:
> Adds new builtin pick(stream) to emit a projection of the input object or array. @pkoppstein #2656
$ jq -n '{"a": 1, "b": {"c": 2, "d": 3}, "e": 4} | pick(.a, .b.c, .x)'
{
"a": 1,
"b": {
"c": 2
},
"x": null
}I ask this as a fan of both the project and the new pick() feature: are there any other functions in jq whose interpretation of their arguments is "non-regular" in the sense the PR author means [here][1]?
More concretely, the expression `.a, .b.c, .x` means something very different outside a pick call. It represents a uncollected stream of more than one value, rather than a reduction of an object into a subset of itself.
The syntax inside a pick call is (I believe) impossible to implement in jq; it has to be a builtin. There's no way to get "regular jq" to extract the path expressions as strings with which to set an object's keys.
Ultimately I do think like the addition, but I recognize the tradeoff of "pure" consistency for syntactical brevity. I'd be curious to hear from the maintainers A) whether I'm off-base about the pathexp syntax limitation and B) if there's some philosophy or design principle guiding why and when they might decide to add specialized interpretations of pathexps.
[1]: https://github.com/jqlang/jq/pull/2656#issuecomment-16228220...
When the jq manual say "path expression" like the argument to pick you can think of it as just normal jq expression but instead outputting values when path/1 is used it will instead output the paths to those values. The limitation is that you only can index into the input (actually including the null value) and don't construct new literals etc.
btw an alternative to pick that might be handy at times is the destruction and shorter object construction syntax:
# use {a:{b:1},c:2} as input
# . as {a:{$b}} will bind $b to 1
# {$b} is shorthand for {b:$b}
# {a} is shorthand for {a:.a}
$ jq -n '{a:{b:1},c:2} | (. as {a:{$b}} | {$b}), {a}'
{
"b": 1
}
{
"a": {
"b": 1
}
} curl https://example.com/api/orders | jq '.data[] | pick(.date, .amount)' curl https://example.com/api/orders | jq '.data[] | {date, amount}'
or curl https://example.com/api/orders | jq '[.data[] | {date, amount}]'
if you want it collected back into an array?I think it'd be very nice to have a bash-like shell with a) jq-like JSON values in the shell (really, parsed, jv-like[0]), b) syntax for using jq programs in ${...}, something like ${var@jq:.jq.program.here}.
As for jq having pipes, they're a rather different beast, but I take your point.
[0] `jv` is the internal representation of JSON values in jq.
Shell scripting is beyond salvation. Inconsistent, illogical crap. It's ridiculous that Bash doesn't have first-class JSON support in 2023, but even if it did, that still wouldn't make me want to write Shell.
> 2. Expect the output of every program to become the input to another, as yet unknown, program. Don't clutter output with extraneous information. Avoid stringently columnar or binary input formats. Don't insist on interactive input.
> 3. Design and build software, even operating systems, to be tried early, ideally within weeks. Don't hesitate to throw away the clumsy parts and rebuild them.
> 4. Use tools in preference to unskilled help to lighten a programming task, even if you have to detour to build the tools and expect to throw some of them out after you've finished using them.
https://en.wikipedia.org/wiki/Unix_philosophy
(No, it is not an indictment of bash. This is how tools are supposed to work together.)