December 16, 2019

flatten json to paths

the following will take the output from a json object and flatten it into a key-value path. this is particularly handy when you’re looking to parse out the supported paths in a given device.

note: you need to remove the \s from this output and put the entire jq string on the same line.

jq '[leaf_paths as $path \
      | {"key": $path | join("/"), "value": getpath($path)}] \
      | from_entries' foo.json

alias to make life happy.

alias json2path="jq '[leaf_paths as \$p | {'key': \$p | join(\"/\"), 'value': getpath(\$p)}] | from_entries'"

Creative Commons License