Jq: Difference between revisions
From wikinotes
(→Usage) |
|||
Line 38: | Line 38: | ||
If you'll never use <code>#</code> in keys/values you could do the following: | If you'll never use <code>#</code> in keys/values you could do the following: | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
echo ' | echo '{ | ||
{ | |||
"one": 1, # foo bar baz | "one": 1, # foo bar baz | ||
# description of key | # description of key | ||
"two": 2 | "two": 2 | ||
}' | sed 's/#.*$??' | jq '.one' | }' \ | ||
| sed 's/#.*$??' \ | |||
| jq '.one' | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Comments --> | </blockquote><!-- Comments --> | ||
</blockquote><!-- Usage --> | </blockquote><!-- Usage --> |
Revision as of 15:25, 28 August 2021
parse json on the commandline.
Documentation
official docs https://stedolan.github.io/jq/manual/ homepage https://stedolan.github.io/jq/
Usage
Basics
# print w/ syntaxhighlighting echo '{"one": 1, "two": {"a": "A"}}' | jq # get key ["one"] echo '{"one": 1, "two": {"a": "A"}}' | jq '.one' # 1 # get nested-key ["two"]["a"] echo '{"one": 1, "two": {"a": "A"}}' | jq '.two.a' # "A" # get list item at index 1 echo '["a", "b", "c"]' | jq '.[1]' # "b"Comments
Neither json nor
jq
support comments.
You could pre-process the object to strip them, however.If you'll never use
#
in keys/values you could do the following:echo '{ "one": 1, # foo bar baz # description of key "two": 2 }' \ | sed 's/#.*$??' \ | jq '.one'