๐ก jq CLI Sheet: JSON Data Filtering & Slicing โ Executive Summary
Technical architectural analysis, practical configuration manifests, and deployment instructions for jq CLI Sheet: JSON Data Filtering & Slicing.
Exec
main
bash
#json to pretty
$jsonSTDOUT | jq ''
echo '\"foo\":\"bar\"' | jq ''
curl http://foo.bar.com/api/v1/test | jq ''
#๋ฐฐ์ด ๋ด ํน์ ์๋ฒ์ ์ถ๋ ฅ
$jsonSTDOUT | jq '.[$n]'
#๋ฐฐ์ด ๋ด ๋ชจ๋ ์์ ๋ด์ ํน์ ํค๋ฅผ ์ถ๋ ฅ
$jsonSTDOUT | jq '.[].$key'
$jsonSTDOUT | jq '.[].$key1.$key2'
#์์ํ ์ถ๋ ฅ์ quote ์ ๊ฑฐ
$jsonSTDOUT | jq -r ''
query
bash
#๋ฐฐ์ด ๋ด ๋ชจ๋ ์์์ค queryํ ํค์ ๋ฐธ๋ฅ๊ฐ ์ผ์นํ ๋์ ์ถ๋ ฅ
$jsonSTDOUT | jq '.[] | select(.$key == "$value")'
#์ด์ค ๋ฆฌ์คํธ ํํฐ๋ง
$jsonSTDOUT | jq '.token.catalog[] | {type: .type, name: .name, ep: (.endpoints[] | select(.interface == "public") | .url) }'
command
bash
#path ํ์ธ
$jsonSTDOUT | jq 'paths'
$jsonSTDOUT | jq -c 'paths'
#์ธํํ์ผ ํ์ธ
$jsonSTDOUT | jq 'input_filename'
#key ๋ฐฐ์ด ์ถ๋ ฅ
$jsonSTDOUT | jq '$json_path | keys'
flags
bash
#์ถ๋ ฅ ์์ถ
-c compact instead of pretty-printed output;
#
-n use `null` as the single input value;
#
-e set the exit status code based on the output;
-s read (slurp) all inputs into an array; apply filter to it;
#"" ์ ๊ฑฐ
-r output raw strings, not JSON texts;
-R read raw strings, not JSON texts;
-C colorize JSON;
-M monochrome (don't colorize JSON);
-S sort keys of objects on output;
--tab use tabs for indentation;
--arg a v set variable $a to value <v>;
--argjson a v set variable $a to JSON value <v>;
--slurpfile a f set variable $a to an array of JSON texts read from <f>;
--rawfile a f set variable $a to a string consisting of the contents of <f>;
--args remaining arguments are string arguments, not files;
--jsonargs remaining arguments are JSON arguments, not files;
-- terminates argument processing;
Features
Check which element of the array has the same key value
bash
#๊ฒฐ๊ณผ๋ก ์ถ๋ ฅ๋ LineNumber -1์ด ๋ฐฐ์ด์ ์์๊ฐ ๋๋ค.
$jsonSTDOUT | jq -r '.[].$key' | grep -n '$value'
Output index.yaml
bash
# .entries ์๋์ ์๋ ๋ชจ๋ key๊ฐ์ ๋ฐฐ์ด๋ก ์ถ๋ ฅ
cat index.yaml | yq -o json | jq '.entries | keys'
# .entries ์๋์ ์๋ ๋ชจ๋ key๊ฐ์ ๋ฐฐ์ด๋ก ์ถ๋ ฅ ํ ๋ฐฐ์ด๋ด์ฉ๋ฌผ๋ง ์ถ๋ ฅ ๋ฐ double-quotes ์ ๊ฑฐ
cat index.yaml | yq -o json | jq '.entries | keys[]' -r
Processed and output in specific JSON format
bash
cat cmp.yaml | yq -ojson | jq -r '{"name":.metadata.name, "path":.spec.source.path}'




Leave a Reply