jq · lesson 28 of 32 · Variables & functions
Recursion: .., paths, walk
.. visits every value in the document, at any depth. Pair it with a type filter such as numbers or strings. walk(f) rewrites every value, children first.
Examples
Example 1Every number, anywhere
Document
{ "a": [1, [2, 3]], "b": { "c": "x" } }Query
[.. | numbers]
..Recursive descent: emit the document and everything nested inside it, at any depth.numbersA filter that lets only numbers through.
Result
[1,2,3]
Example 2Rewrite every number
Document
{ "a": [1, [2, 3]], "b": { "c": "x" } }Query
walk(if type == "number" then . * 10 else . end)
walk(Apply the filter to every value in the document, children first.if type == "number"Test each value's type…then . * 10 else . end…multiply numbers by 10 and leave everything else unchanged.
Result
{"a":[10,[20,30]],"b":{"c":"x"}}Good to know
[paths]lists every location as an array of keys and indexes —["a"],["a",0], … — andgetpath(["a",0])reads one.