Skip to content
Latchkey

jq walk, paths and getpath : Deep JSON Edits

jq walk(f) applies f to every value in a nested structure bottom up, paths enumerates every path into a value, and getpath([..]) reads a value at a runtime-built path.

When an API response is deeply nested, walk normalizes the whole payload at once, paths shows you how to address a field, and getpath reads a computed location in CI.

What it does

walk(f) recurses through arrays and objects depth-first, applying f to each value after its children. paths outputs each path as an array of keys/indices, with paths(scalars) or leaf_paths limited to leaves. getpath(["a","b"]) reads the value at that path, the dynamic counterpart of .a.b, and setpath assigns at a computed path. walk is in the standard library, so a very old jq build may lack it.

Common usage

Terminal
# delete every null-valued key, recursively
jq 'walk(if type == "object" then with_entries(select(.value != null)) else . end)' resp.json
# lowercase every string in the tree
jq 'walk(if type == "string" then ascii_downcase else . end)' resp.json
# enumerate leaf paths, then read one dynamically
gh api repos/cli/cli --jq 'leaf_paths'
jq 'getpath(["owner","login"])' repo.json

Functions

FormWhat it does
walk(f)Apply f to every value, depth-first
paths / leaf_pathsAll paths, or only paths to leaf scalars
getpath([...])Read value at a dynamic path
setpath([...]; v)Set value at a dynamic path
typeBranch f on "object", "array", "string", etc.

In CI

Use walk to scrub a payload once rather than chaining many targeted edits, guarding each branch on type. Use paths to discover how to address a deep field and getpath to read a location built from variables.

Common errors in CI

"jq: error: walk/1 is not defined" means a jq older than 1.6 without the builtin; upgrade jq or paste the def into the program. "jq: error: Cannot iterate over null (null)" from paths means the input was null. "jq: error: Cannot index string with \"x\"" from getpath means a path segment hit a scalar; getpath on a missing path returns null rather than erroring, so guard the result.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →