Skip to content
Myr

builtin: add pick(stream)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

pick(stream) works on both arrays and objects Restrictions on the dot-paths are specified in manual.yml See #2578

authored by pkoppstein · committed by Nico Williams · parent a881532 · Browse files · 3 files+32 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+32 added−0 removed

  • tests:+8 added−0 removed
  • other:+24 added−0 removed

3 files+32 added−0 removed

modified:docs/content/manual/manual.yml+18 added
923 unmodified lines
924 - program: 'map_values(.+1)'925 input: '{"a": 1, "b": 2, "c": 3}'926 output: ['{"a": 2, "b": 3, "c": 4}']927928 - title: "`pick(stream)`"929 body: |930 931 Emit the projection of the input object or array defined by the specified stream of932 dot-path specifications, such that if p is one of these specifications,933 and `(. | p)` evaluates to $x, then `pick(stream) | p` will also be $x.934 For arrays, negative indices and .[m:n] specifications should not be used.935936 examples:937 - program: 'pick(.a, .b.c, .x)'938 input: '{"a": 1, "b": {"c": 2, "d": 3}, "e": 4}'939 output: ['{"a":1,"b":{"c":2},"x":null}']940941 - program: '[1,2,3,4] | pick(.[2], .[0], .[0])'942 input: '[1,2,3,4]'943 output: ['[1,null,3]']944945946 - title: "`path(path_expression)`"947 body: |948949 Outputs array representations of the given path expression
2516 unmodified lines
modified:src/builtin.jq+6 added
258 unmodified lines
259 elif type == "array" then map( walk(f) ) | f260 else f261 end;262263# stream should be a stream of dot-paths264def pick(stream):265 . as $in266 | reduce path(stream) as $a (null;267 setpath($a; $in|getpath($a)) );268269# SQL-ish operators here:270def INDEX(stream; idx_expr):271 reduce stream as $row ({}; .[$row|idx_expr|tostring] = $row);272def INDEX(idx_expr): INDEX(.[]; idx_expr);
8 unmodified lines
modified:tests/man.test+8 added
217 unmodified lines
218map_values(.+1)219{"a": 1, "b": 2, "c": 3}220{"a": 2, "b": 3, "c": 4}221222pick(.a, .b.c, .x)223{"a": 1, "b": {"c": 2, "d": 3}, "e": 4}224{"a":1,"b":{"c":2},"x":null}225226[1,2,3,4] | pick(.[2], .[0], .[0])227[1,2,3,4]228[1,null,3]229230path(.a[0].b)231null232["a",0,"b"]233
665 unmodified lines