linux下json解析神器----jq
前言
在linux環境中,使用curl命令,呼叫單個介面,返回的資料通常都是一大坨,看起來很不方便。
如圖:

curl.png
如果我們只需要其中的一部分資料,name在這麼一大坨中尋找,還是比較吃力的。
一般遇到這種情況,可以把response拷貝下來,利用工具,格式化JSON。

Json.png
介紹一款神器,直接在linux中格式化JSON
JSON解析神器----jq
1.jq簡介
首先看下一段摘抄自網上的介紹
jq可以對json資料進行分片、過濾、對映和轉換,和sed、awk、grep等命令一樣,都可以讓你輕鬆地把玩文字。它能輕鬆地把你擁有的資料轉換成你期望的格式,而且需要寫的程式通常也比你期望的更加簡短。
jq的官方地址 : ofollow,noindex">jq
2.示例
2.1 .
這應該是最簡單的篩選,只是簡單的將結果格式化
The absolute simplest filter is . . This is a filter that takes its input and produces it unchanged as output. That is, this is the identity operator.Since jq by default pretty-prints all output, this trivial program can be a useful way of formatting JSON output from, say, curl.
curl -s-X POST -d '"uid":"125778302"}' http://localhost/lock_screen | jq . { "code": 0, "err_string": "", "user_id": "125778302", "docs": [ 10032243, 10032242, 10032240, 10032239, 10032231, 10032230, 10032217, 10032212 ], "props": "{\"10032212\":{\"from\":1036},\"10032217\":{\"from\":1036},\"10032230\":{\"from\":1036},\"10032231\":{\"from\":1036},\"10032239\":{\"from\":1036},\"10032240\":{\"from\":1036},\"10032242\":{\"from\":1036},\"10032243\":{\"from\":1036}}", "request_id": "1537520169462932797678" }
為了不涉密,把請求中的引數都抹去了,可以看出,響應已經完成了格式化
2 過濾 .foo
如果我們想只看docs這個列表,不需要看其他資訊,那應該怎麼做?
只需要在.後面加上 對應的key值
curl -s-X POST -d '"uid":"125778302"}' http://localhost/lock_screen | jq .docs [ 10032243, 10032242, 10032240, 10032239, 10032231, 10032230, 10032217, 10032212 ]
3.切片 .foo[index]
jq同樣也支援切片
curl -s-X POST -d '"uid":"125778302"}' http://localhost/lock_screen | jq .docs[1] 10032242 curl -s-X POST -d '"uid":"125778302"}' http://localhost/lock_screen | jq .docs[1:4] [ 10032242, 10032240, 10032239 ]
4. .foo[]與.foo[]?與.foo的區別
對於docs來說,它的value是一個列表
- .docs 輸出的是一個整體
curl -s-X POST -d '"uid":"125778302"}' http://localhost/lock_screen | jq .docs [ 10032243, 10032242, 10032240, 10032239, 10032231, 10032230, 10032217, 10032212 ]
- .docs[]輸出的8個數字,而不是一個整體
curl -s-X POST -d '"uid":"125778302"}' http://localhost/lock_screen | jq .docs[] 10032243 10032242 10032240 10032239 10032231 10032230 10032217 10032212
If you use the .[index] syntax, but omit the index entirely, it will return all of the elements of an array. Running .[] with the input [1,2,3] will produce the numbers as three separate results, rather than as a single array.You can also use this on an object, and it will return all the values of the object.
-
.[]與.[]?的區別
官網文件中有這麼一句話
Like .[], but no errors will be output if . is not an array or object.
即: []會有報錯,[]?沒有報錯
實踐一下:
props的value是一個字串
curl -s-X POST -d '"uid":"125778302"}' http://localhost/lock_screen | jq .props[] jq: error (at <stdin>:0): Cannot iterate over string ("{\"1003221...) curl -s-X POST -d '"uid":"125778302"}' http://localhost/lock_screen | jq .props[]? 沒有任何輸出
5.輸出多個引數,
用,隔離需要輸出的引數
curl -s-X POST -d '"uid":"125778302"}' http://localhost/lock_screen | jq .docs,.user_id [ 10032243, 10032242, 10032240, 10032239, 10032231, 10032230, 10032217, 10032212 ] "125778302"
6.自定義key
由上可知,雖然輸出了value但是key值丟失了,如果想要輸出key怎麼辦?
url xxxxxxxjq '.|{dddd:.docs ,uuuu: .user_id}' { "dddd": [ 10032217, 10032232, 10032240, 10032219, 10032228, 10032230, 10032234, 10032231, 10032220, 10032243 ], "uuuu": "125778302" }
更多用法,參照官網 wiki文件