步遥情感网
您的当前位置:首页linux json 工具,linux下json解析神器----jq

linux json 工具,linux下json解析神器----jq

来源:步遥情感网

前言

在linux环境中,使用curl命令,调用单个接口,返回的数据通常都是一大坨,看起来很不方便。

如图:

curl.png

如果我们只需要其中的一部分数据,name在这么一大坨中寻找,还是比较吃力的。

一般遇到这种情况,可以把response拷贝下来,利用工具,格式化JSON。

Json.png

介绍一款神器,直接在linux中格式化JSON

JSON解析神器----jq

1.jq简介

首先看下一段摘抄自网上的介绍

jq可以对json数据进行分片、过滤、映射和转换,和sed、awk、grep等命令一样,都可以让你轻松地把玩文本。它能轻松地把你拥有的数据转换成你期望的格式,而且需要写的程序通常也比你期望的更加简短。

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 :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 xxxxxxx jq '.|{dddd:.docs ,uuuu: .user_id}'

{

"dddd": [

10032217,

10032232,

10032240,

10032219,

10032228,

10032230,

10032234,

10032231,

10032220,

10032243

],

"uuuu": "125778302"

}

更多用法,参照官网wiki文档

因篇幅问题不能全部显示,请点此查看更多更全内容