Jq json file manipulation: Difference between revisions
Jump to navigation
Jump to search
Created page with "viewing the top level structure for all objects <pre> jq '.[]|keys' filename </pre> viewing the top level structure for the first object <pre> jq '.[0]|keys' filename </pre> v..." |
No edit summary |
||
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
viewing the top level structure for all objects | viewing the top level structure for all objects | ||
<pre> | |||
jq '[path(..)|map(if type=="number" then "[]" else tostring end)|join(".")|split(".[]")|join("[]")]|unique|map("."+.)|.[]' first10-10.json | |||
</pre> | |||
<pre> | |||
jq -c 'path(..)|[.[]|tostring]|join("/")' first10-10.json | |||
</pre> | |||
<pre> | |||
jq 'path(recurse(if type|. == "array" or . =="object" then .[] else empty end))' first10-10.json | |||
</pre> | |||
<pre> | |||
jq '.[]|path(..)' first10-10.json | |||
</pre> | |||
<pre> | <pre> | ||
jq '.[]|keys' filename | jq '.[]|keys' filename | ||
</pre> | </pre> | ||
Alternatively show_struct.py from https://github.com/ilyash/show-struct shows the structure with a description and the filled in object. | |||
viewing the top level structure for the first object | viewing the top level structure for the first object | ||
<pre> | <pre> | ||
Line 14: | Line 29: | ||
<pre> | <pre> | ||
jq '.[0] | keys | .[0] filename | jq '.[0] | keys | .[0] filename | ||
</pre> | |||
Getting the subkeys (when you know there are subkeys) | |||
<pre> | |||
jq '.[0].data|keys' filename | |||
</pre> | </pre> | ||
Line 24: | Line 43: | ||
getting specific data from a subkey | getting specific data from a subkey | ||
<pre> | <pre> | ||
jq '.[0].id' filename | |||
jq '.[].data.category.name' filename | sort -u | jq '.[].data.category.name' filename | sort -u | ||
</pre> | </pre> | ||
http://blog.librato.com/posts/jq-json | |||
https://shapeshed.com/jq-json/#how-to-map-values | |||
http://www.compciv.org/recipes/cli/jq-for-parsing-json/ |
Latest revision as of 10:37, 17 November 2017
viewing the top level structure for all objects
jq '[path(..)|map(if type=="number" then "[]" else tostring end)|join(".")|split(".[]")|join("[]")]|unique|map("."+.)|.[]' first10-10.json
jq -c 'path(..)|[.[]|tostring]|join("/")' first10-10.json
jq 'path(recurse(if type|. == "array" or . =="object" then .[] else empty end))' first10-10.json
jq '.[]|path(..)' first10-10.json
jq '.[]|keys' filename
Alternatively show_struct.py from https://github.com/ilyash/show-struct shows the structure with a description and the filled in object.
viewing the top level structure for the first object
jq '.[0]|keys' filename
viewing the top level structure for the third object
jq '.[2]|keys' filename
viewing the first key for the first object
jq '.[0] | keys | .[0] filename
Getting the subkeys (when you know there are subkeys)
jq '.[0].data|keys' filename
selecting a specific item (from the top level)
jq '.[] | select(.id=bla)' filename
getting specific data from a subkey
jq '.[0].id' filename jq '.[].data.category.name' filename | sort -u
http://blog.librato.com/posts/jq-json