summaryrefslogtreecommitdiffstats
path: root/builtin.c
diff options
context:
space:
mode:
authorNicolas Williams <nico@cryptonector.com>2014-02-17 12:28:26 -0600
committerNicolas Williams <nico@cryptonector.com>2014-02-20 15:33:07 -0600
commitfe29d3d3fa4a147a5103f314bc09287c4c32a81d (patch)
tree0f4d4c5625016c2631cf6b101436bb8d11bb8501 /builtin.c
parent3d33412e9a88f86510b2dc8afe6ac4e82b1a44df (diff)
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 '|'.
Diffstat (limited to 'builtin.c')
-rw-r--r--builtin.c1
1 files changed, 1 insertions, 0 deletions
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;",