"def _assign(paths; value): value as $v | reduce path(paths) as $p (.; setpath($p; $v));",
"def _modify(paths; update): reduce path(paths) as $p (.; setpath($p; getpath($p) | update));",
"def recurse(f): ., (f | select(. != null) | recurse(f));",
+ "def recurse_down: recurse(.[]?);",
"def to_entries: [keys[] as $k | {key: $k, value: .[$k]}];",
"def from_entries: map({(.key): .value}) | add;",
"def with_entries(f): to_entries | map(f) | from_entries;",
input: '{"foo": 42}'
output: [42]
+ - title: "`.foo?`"
+ body: |
+
+ Just like `.foo`, but does not output even an error when `.`
+ is not an array or an object.
+
+ examples:
+ - program: '.foo?'
+ input: '{"foo": 42, "bar": "less interesting data"}'
+ output: [42]
+ - program: '.foo?'
+ input: '{"notfoo": true, "alsonotfoo": false}'
+ output: ['null']
+ - program: '.["foo"]?'
+ input: '{"foo": 42}'
+ output: [42]
+ - program: '[.foo?]'
+ input: '[1,2]'
+ output: ['[]']
+
- title: "`.[<string>]`, `.[2]`, `.[10:15]`"
body: |
the array), or omitted (in which case it refers to the start
or end of the array).
+ The `?` "operator" can also be used with the slice operator,
+ as in `.[10:15]?`, which outputs values where the inputs are
+ slice-able.
+
examples:
- program: '.[0]'
input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'
input: '{"a": 1, "b": 1}'
output: ['1', '1']
+ - title: "`.[]?`"
+ body: |
+
+ Like `.[]`, but no errors will be output if . is not an array
+ or object.
+
- title: "`,`"
body: |
- '{"foo":[{"foo":[]}]}'
- '{"foo":[]}'
+ - title: `recurse_down`
+ body: |
+
+ A quieter version of `recurse(.[])`, equivalent to:
+
+ def recurse_down: recurse(.[]?);
+
+ - title: `..`
+ body: |
+
+ Short-hand for `recurse_down`. This is intended to resemble
+ the XPath `//` operator. Note that `..a` does not work; use
+ `..|a` instead.
+
+ examples:
+ - program: '..|.a?'
+ input: '[[{"a":1}]]'
+ output: ['1']
- title: "String interpolation - `\(foo)`"
body: |
goto do_backtrack;
}
- case INDEX: {
+ case INDEX:
+ case INDEX_OPT: {
jv t = stack_pop(jq);
jv k = stack_pop(jq);
path_append(jq, jv_copy(k));
if (jv_is_valid(v)) {
stack_push(jq, v);
} else {
- print_error(jq, v);
+ if (opcode == INDEX)
+ print_error(jq, v);
+ else
+ jv_free(v);
goto do_backtrack;
}
break;
}
case EACH:
+ case EACH_OPT:
stack_push(jq, jv_number(-1));
// fallthrough
- case ON_BACKTRACK(EACH): {
+ case ON_BACKTRACK(EACH):
+ case ON_BACKTRACK(EACH_OPT): {
int idx = jv_number_value(stack_pop(jq));
jv container = stack_pop(jq);
int keep_going, is_last = 0;
jv key, value;
if (jv_get_kind(container) == JV_KIND_ARRAY) {
- if (opcode == EACH) idx = 0;
+ if (opcode == EACH || opcode == EACH_OPT) idx = 0;
else idx = idx + 1;
int len = jv_array_length(jv_copy(container));
keep_going = idx < len;
value = jv_array_get(jv_copy(container), idx);
}
} else if (jv_get_kind(container) == JV_KIND_OBJECT) {
- if (opcode == EACH) idx = jv_object_iter(container);
+ if (opcode == EACH || opcode == EACH_OPT) idx = jv_object_iter(container);
else idx = jv_object_iter_next(container, idx);
keep_going = jv_object_iter_valid(container, idx);
if (keep_going) {
value = jv_object_iter_value(container, idx);
}
} else {
- assert(opcode == EACH);
- print_error(jq, jv_invalid_with_msg(jv_string_fmt("Cannot iterate over %s",
- jv_kind_name(jv_get_kind(container)))));
+ assert(opcode == EACH || opcode == EACH_OPT);
+ if (opcode == EACH) {
+ print_error(jq,
+ jv_invalid_with_msg(jv_string_fmt("Cannot iterate over %s",
+ jv_kind_name(jv_get_kind(container)))));
+ }
keep_going = 0;
}
"//=" { return SETDEFINEDOR; }
"<=" { return LESSEQ; }
">=" { return GREATEREQ; }
-"."|"="|";"|","|":"|"|"|"+"|"-"|"*"|"/"|"%"|"\$"|"<"|">" { return yytext[0];}
+".." { return REC; }
+"."|"?"|"="|";"|","|":"|"|"|"+"|"-"|"*"|"/"|"%"|"\$"|"<"|">" { return yytext[0];}
"["|"{"|"(" {
return enter(yytext[0], YY_START, yyscanner);
OP(LOADVN, VARIABLE, 1, 1)
OP(STOREV, VARIABLE, 1, 0)
OP(INDEX, NONE, 2, 1)
+OP(INDEX_OPT, NONE, 2, 1)
OP(EACH, NONE, 1, 1)
+OP(EACH_OPT, NONE, 1, 1)
OP(FORK, BRANCH, 0, 0)
OP(JUMP, BRANCH, 0, 0)
OP(JUMP_F,BRANCH, 1, 0)
%token <literal> FIELD
%token <literal> LITERAL
%token <literal> FORMAT
+%token Q "?"
+%token REC ".."
%token SETMOD "%="
%token EQ "=="
%token NEQ "!="
return BLOCK(gen_subexp(key), obj, gen_op_simple(INDEX));
}
-static block gen_slice_index(block obj, block start, block end) {
+static block gen_index_opt(block obj, block key) {
+ return BLOCK(gen_subexp(key), obj, gen_op_simple(INDEX_OPT));
+}
+
+static block gen_slice_index(block obj, block start, block end, opcode idx_op) {
block key = BLOCK(gen_subexp(gen_const(jv_object())),
gen_subexp(gen_const(jv_string("start"))),
gen_subexp(start),
gen_subexp(gen_const(jv_string("end"))),
gen_subexp(end),
gen_op_simple(INSERT));
- return BLOCK(key, obj, gen_op_simple(INDEX));
+ return BLOCK(key, obj, gen_op_simple(idx_op));
}
static block gen_binop(block a, block b, int op) {
'.' {
$$ = gen_noop();
} |
+REC {
+ $$ = gen_call("recurse_down", gen_noop());
+} |
+Term FIELD '?' {
+ $$ = gen_index_opt($1, gen_const($2));
+} |
+FIELD '?' {
+ $$ = gen_index_opt(gen_noop(), gen_const($1));
+} |
+Term '.' String '?' {
+ $$ = gen_index_opt($1, $3);
+} |
+'.' String '?' {
+ $$ = gen_index_opt(gen_noop(), $2);
+} |
Term FIELD {
$$ = gen_index($1, gen_const($2));
} |
$$ = gen_noop();
} |
/* FIXME: string literals */
+Term '[' Exp ']' '?' {
+ $$ = gen_index_opt($1, $3);
+} |
Term '[' Exp ']' {
$$ = gen_index($1, $3);
} |
+Term '[' ']' '?' {
+ $$ = block_join($1, gen_op_simple(EACH_OPT));
+} |
Term '[' ']' {
$$ = block_join($1, gen_op_simple(EACH));
} |
+Term '[' Exp ':' Exp ']' '?' {
+ $$ = gen_slice_index($1, $3, $5, INDEX_OPT);
+} |
+Term '[' Exp ':' ']' '?' {
+ $$ = gen_slice_index($1, $3, gen_const(jv_null()), INDEX_OPT);
+} |
+Term '[' ':' Exp ']' '?' {
+ $$ = gen_slice_index($1, gen_const(jv_null()), $4, INDEX_OPT);
+} |
Term '[' Exp ':' Exp ']' {
- $$ = gen_slice_index($1, $3, $5);
+ $$ = gen_slice_index($1, $3, $5, INDEX);
} |
Term '[' Exp ':' ']' {
- $$ = gen_slice_index($1, $3, gen_const(jv_null()));
+ $$ = gen_slice_index($1, $3, gen_const(jv_null()), INDEX);
} |
Term '[' ':' Exp ']' {
- $$ = gen_slice_index($1, gen_const(jv_null()), $4);
+ $$ = gen_slice_index($1, gen_const(jv_null()), $4, INDEX);
} |
LITERAL {
$$ = gen_const($1);
{"foo": {"bar": 20}}
20
+[.[]|.foo?]
+[1,[2],{"foo":3,"bar":4},{},{"foo":5}]
+[3,null,5]
+
+[.[]|.foo?.bar?]
+[1,[2],[],{"foo":3},{"foo":{"bar":4}},{}]
+[4,null]
+
+[..]
+[1,[[2]],{ "a":[1]}]
+[[1,[[2]],{"a":[1]}],1,[[2]],[2],2,{"a":[1]},[1],1]
+
+[.[]|.[]?]
+[1,null,[],[1,[2,[[3]]]],[{}],[{"a":[1,[2]]}]]
+[1,[2,[[3]]],{},{"a":[1,[2]]}]
+
+[.[]|.[1:3]?]
+[1,null,true,false,"abcdef",{},{"a":1,"b":2},[],[1,2,3,4,5],[1,2]]
+[null,"bc",[],[2,3],[2]]
#
# Multiple outputs, iteration