前言
在linux環(huán)境中,使用curl命令孵睬,調(diào)用單個(gè)接口,返回的數(shù)據(jù)通常都是一大坨伶跷,看起來(lái)很不方便掰读。
如圖:
如果我們只需要其中的一部分?jǐn)?shù)據(jù),name在這么一大坨中尋找叭莫,還是比較吃力的蹈集。
一般遇到這種情況,可以把response拷貝下來(lái)雇初,利用工具拢肆,格式化JSON。
介紹一款神器靖诗,直接在linux中格式化JSON
JSON解析神器----jq
1.jq簡(jiǎn)介
首先看下一段摘抄自網(wǎng)上的介紹
jq可以對(duì)json數(shù)據(jù)進(jìn)行分片郭怪、過(guò)濾、映射和轉(zhuǎn)換,和sed刊橘、awk鄙才、grep等命令一樣,都可以讓你輕松地把玩文本。它能輕松地把你擁有的數(shù)據(jù)轉(zhuǎn)換成你期望的格式,而且需要寫(xiě)的程序通常也比你期望的更加簡(jiǎn)短促绵。
jq的官方地址 :jq
2.示例
2.1 .
這應(yīng)該是最簡(jiǎn)單的篩選攒庵,只是簡(jiǎn)單的將結(jié)果格式化
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"
}
為了不涉密,把請(qǐng)求中的參數(shù)都抹去了败晴,可以看出浓冒,響應(yīng)已經(jīng)完成了格式化
2 過(guò)濾 .foo
如果我們想只看docs這個(gè)列表,不需要看其他信息尖坤,那應(yīng)該怎么做裆蒸?
只需要在.后面加上 對(duì)應(yīng)的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的區(qū)別
對(duì)于docs來(lái)說(shuō),它的value是一個(gè)列表
- .docs 輸出的是一個(gè)整體
curl -s -X POST -d '"uid":"125778302"}' http://localhost/lock_screen | jq .docs
[
10032243,
10032242,
10032240,
10032239,
10032231,
10032230,
10032217,
10032212
]
- .docs[]輸出的8個(gè)數(shù)字糖驴,而不是一個(gè)整體
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.
- .[]與.[]?的區(qū)別
官網(wǎng)文檔中有這么一句話
Like .[], but no errors will be output if . is not an array or object.
即: []會(huì)有報(bào)錯(cuò)僚祷,[]佛致?沒(méi)有報(bào)錯(cuò)
實(shí)踐一下:
props的value是一個(gè)字符串
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[]?
沒(méi)有任何輸出
5.輸出多個(gè)參數(shù),
用,隔離需要輸出的參數(shù)
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 xxxxxxx jq '.|{dddd:.docs ,uuuu: .user_id}'
{
"dddd": [
10032217,
10032232,
10032240,
10032219,
10032228,
10032230,
10032234,
10032231,
10032220,
10032243
],
"uuuu": "125778302"
}