You can also use the following jq command to flatten nested JSON objects in this manner:
[leaf_paths as $path | {"key": $path | join("."), "value": getpath($path)}] | from_entries
The way it works is: leaf_paths
returns a stream of arrays which represent the paths on the given JSON document at which "leaf elements" appear, that is, elements which do not have child elements, such as numbers, strings and booleans. We pipe that stream into objects with key
and value
properties, where key
contains the elements of the path array as a string joined by dots and value
contains the element at that path. Finally, we put the entire thing in an array and run from_entries
on it, which transforms an array of {key, value}
objects into an object containing those key-value pairs.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…