4 ms·
Hey, pick is implemented in jq here https://github.com/jqlang/jq/blob/master/src/builtin.jq#L258-L262 https://github.com/jqlang/jq/blob/master/src/builtin.jq#L2
by wwader 3y ago
Hey, pick is implemented in jq here https://github.com/jqlang/jq/blob/master/src/builtin.jq#L258-L262 https://github.com/jqlang/jq/blob/master/src/builtin.jq#L258... using various path functions.
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
}
}