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]
  1. .. Recursive descent: emit the document and everything nested inside it, at any depth.
  2. numbers A filter that lets only numbers through.

Result

[1,2,3]

Try it in the playground

Example 2Rewrite every number

Document

{ "a": [1, [2, 3]], "b": { "c": "x" } }

Query

walk(if type == "number" then . * 10 else . end)
  1. walk( Apply the filter to every value in the document, children first.
  2. if type == "number" Test each value's type…
  3. then . * 10 else . end …multiply numbers by 10 and leave everything else unchanged.

Result

{"a":[10,[20,30]],"b":{"c":"x"}}

Try it in the playground

Good to know

  • [paths] lists every location as an array of keys and indexes — ["a"], ["a",0], … — and getpath(["a",0]) reads one.

About jsonquery

Query, format and learn JSON with jq, JSONPath, JMESPath and JSON Pointer. It is the engine of the desktop app, run in your browser.

Engine
The desktop app’s, compiled to WebAssembly
Licence
MIT (the engine and the app)
Source
GitHub
Contact
The contact form (private)
Public issues
GitHub
Legal
Privacy policy

Known limitations: a file over 10 MB opens read-only and one over 512 MB does not open here; the desktop app has no such limit.