From fe29d3d3fa4a147a5103f314bc09287c4c32a81d Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Mon, 17 Feb 2014 12:28:26 -0600 Subject: Add `?`, `.[]?`, and `..` operators Make XPath-like `//a/b` recursive structure traversal easier in jq, which then becomes: ..|.a?.b? The `?` operator suppresses errors about . not being an array or object. The `..` operator is equivalent to calling the new `recurse_down` built-in, which in turn is equivalent to recurse(.[]?) Note that `..a` is not supported; neither is `...a`. That could be add added, but it doesn't seem worth the trouble of saving the need to type a '|'. --- builtin.c | 1 + 1 file changed, 1 insertion(+) (limited to 'builtin.c') diff --git a/builtin.c b/builtin.c index ddf66d00..e60fa85b 100644 --- a/builtin.c +++ b/builtin.c @@ -672,6 +672,7 @@ static const char* const jq_builtins[] = { "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;", -- cgit v1.2.3