jq命令幫助我們很方便地在終端查看和處理json文件
jq命令的幫助信息:
abby@abby:bgs$ jq -h jq - commandline JSON processor [version 1.5-1-a5b5cbe] Usage: jq [options] <jq filter> [file...] jq is a tool for processing JSON inputs, applying the given filter to its JSON text inputs and producing the filter's results as JSON on standard output. The simplest filter is ., which is the identity filter, copying jq's input to its output unmodified (except for formatting). For more advanced filters see the jq(1) manpage ("man jq") and/or https://stedolan.github.io/jq Some of the options include: -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>; See the manpage for more options.
#默認情況下,jq會將json格式化為多行樹狀結構輸出,但有時需要將一個json串在一行輸出,可使用-c參數,例如
jq -c . xxx.json
#用逗號分隔可以同時獲取json中多個key的值。但過濾出的多個值會分多行顯示。要注意除了逗號之外不能有其他字符(包括空格),例如
js .field1,.field2 xxx.json
使用:
$ jq . xxx.json # 直接將文件名傳給 jq $ cat bgs_enterprise_info_norm.json | jq . #或者由其他命令讀出文件內容,並傳給 jq
例子:
原始數據:
abby@abby:bgs$ head -n2 bgs_enterprise_info_norm.json {"industry": "紡織服裝、服飾業", "name": "xxx公司", "business": "xxxxx", "description": "空值"} {"industry": "批發業", "name": "xxx公司", "business": "xxxxx", "description": "空值"}
格式化:
abby@abby:bgs$ head -n2 bgs_enterprise_info_norm.json | jq . { "industry": "紡織服裝、服飾業", "name": "xxx公司", "business": "xxxxx", "description": "空值" } { "industry": "批發業", "name": "xxx公司", "business": "xxxxx", "description": "空值" }
選取某屬性:
abby@abby:bgs$ head -n2 bgs_enterprise_info_norm.json | jq .industry "紡織服裝、服飾業" "批發業"
abby@abby:bgs$ jq .description bgs_enterprise_info_norm.json | grep 空值 | wc -l 2770786
內建函數
jq還有一些內建函數如 key,has
key是用來獲取JSON中的key元素的:
cat json_raw.txt | jq 'keys' [ "employees", "location", "name" ]
has是用來是判斷是否存在某個key:
cat json_raw.txt | jq 'has("name")' true cat json_raw.txt | jq 'has("noexisted")' false
參考: