]> granicus.if.org Git - jq/commitdiff
Add the `recurse` function. See #37.
authorStephen Dolan <mu@netsoc.tcd.ie>
Sat, 29 Dec 2012 22:59:07 +0000 (22:59 +0000)
committerStephen Dolan <mu@netsoc.tcd.ie>
Sat, 29 Dec 2012 22:59:07 +0000 (22:59 +0000)
builtin.c
docs/content/3.manual/manual.yml

index b0c6c34c07eca40c59bd77675e3418c8d0de5271..5818a6a41f198c4e9eed5dc6acb30094985e7fee 100644 (file)
--- a/builtin.c
+++ b/builtin.c
@@ -544,7 +544,7 @@ static const char* jq_builtins[] = {
   "def del(f): delpaths([path(f)]);",
   "def _assign(paths; value): value as $v | fold . as $obj (path(paths) as $p | $obj | setpath($p; $v));",
   "def _modify(paths; update): fold . as $obj (path(paths) as $p | $obj | setpath($p; getpath($p) | update));",
-  
+  "def recurse(f): ., (f | select(. != null) | recurse(f));",
 };
 
 
index fba7e4a72aa99129947b6df5247e6152fa996d16..de6082105ac25dc42513374c79c16edbbd84c369 100644 (file)
@@ -643,6 +643,38 @@ sections:
             input: '{"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]}'
             output: ['false']
 
+      - title: `recurse`
+        body: |
+          
+          The `recurse` function allows you to search through a
+          recursive structure, and extract interesting data from all
+          levels. Suppose your input represents a filesystem:
+
+              {"name": "/", "children": [
+                {"name": "/bin", "children": [
+                  {"name": "/bin/ls", "children": []},
+                  {"name": "/bin/sh", "children": []}]},
+                {"name": "/home", "children": [
+                  {"name": "/home/stephen", "children": [
+                    {"name": "/home/stephen/jq", "children": []}]}]}]}
+          
+          Now suppose you want to extract all of the filenames
+          present. You need to retrieve `.name`, `.children[].name`,
+          `.children[].children[].name`, and so on. You can do this
+          with:
+
+              recurse(.children[]) | .name
+
+        examples:
+          - program: 'recurse(.foo[])'
+            input: '{"foo":[{"foo": []}, {"foo":[{"foo":[]}]}]}'
+            output: 
+              - '{"foo":[{"foo":[]},{"foo":[{"foo":[]}]}]}'
+              - '{"foo":[]}'
+              - '{"foo":[{"foo":[]}]}'
+              - '{"foo":[]}'
+
+
       - title: "String interpolation - `\(foo)`"
         body: |