linux下json解析神器----jq

前言

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


curl.png

如果我们只需要其中的一部分数据,name在这么一大坨中寻找,还是比较吃力的。
一般遇到这种情况,可以把response拷贝下来,利用工具,格式化JSON。


Json.png

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

JSON解析神器----jq

1.jq简介

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

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

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

{
  "dddd": [
    10032217,
    10032232,
    10032240,
    10032219,
    10032228,
    10032230,
    10032234,
    10032231,
    10032220,
    10032243
  ],
  "uuuu": "125778302"
}
更多用法,参照官网wiki文档
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,350评论 19 139
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,828评论 0 13
  • Overview The ccxt library is a collection of available cr...
    郭蝈儿蝈儿阅读 3,980评论 0 1
  • Getting Started Use the Current Stable Version (7.1) Buil...
    Leonzai阅读 2,040评论 0 3
  • 1.需求分析:明确系统功能;进行可行性分析。 2.总体设计:出软件总体结构图并进行功能模块划分。 3.概要设计:对...
    冷面水手阅读 270评论 0 1

友情链接更多精彩内容