博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSON 命令行工具
阅读量:5302 次
发布时间:2019-06-14

本文共 3802 字,大约阅读时间需要 12 分钟。

原文链接:

JQ

jq 是一个处理 JSON 数据的 LINUX 命令行工具.

命令帮助

jq - commandline JSON processor [version 1.5-1-a5b5cbe]Usage: jq [options] 
[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
; --argjson a v set variable $a to JSON value
; --slurpfile a f set variable $a to an array of JSON texts read from
; See the manpage for more options.

常用场景

curl -s https://api.wxaxiaoyao.cn/api/v0/user/1{  "id": 1,  "username": "xiaoyao",  "email": "765485868@qq.com",  "cellphone": "187*******",  "nickname": "逍遥",  "portrait": "https://statics.qiniu.wxaxiaoyao.cn/_/portraits/x1.png",  "sex": "男",  "motto": "世间有异贾, 专售荒唐梦, 以慰失意人, 闻者购如风. 莫问梦醒时, 图乐在梦中, 人生是何物? 百年一场梦.",  "description": "世间有异贾,专售荒唐梦,以慰失意人,闻者购如风。莫问梦醒时,图乐在梦中,人生是何物?百年一场梦.",  "location": "广东省深圳市南山区粤海街道高新区高新南一道德赛大厦23层(2303-2306)",  "roleId": 0,  "address": null,  "createdAt": "2018-09-12T08:13:12.000Z",  "updatedAt": "2019-08-08T01:37:13.000Z"}
  1. 缩进输出
curl -s https://api.wxaxiaoyao.cn/api/v0/user/1 > demo.json   # 获取测试 JSON, 内容如下:curl -s https://api.wxaxiaoyao.cn/api/v0/user/1 | jq .{  "id": 1,  "username": "xiaoyao",  "email": "765485868@qq.com",  "cellphone": "187*******",  "nickname": "逍遥",  "portrait": "https://statics.qiniu.wxaxiaoyao.cn/_/portraits/x1.png",  "sex": "男",  "motto": "世间有异贾, 专售荒唐梦, 以慰失意人, 闻者购如风. 莫问梦醒时, 图乐在梦中, 人生是何物? 百年一场梦.",  "description": "世间有异贾,专售荒唐梦,以慰失意人,闻者购如风。莫问梦醒时,图乐在梦中,人生是何物?百年一场梦.",  "location": "广东省深圳市南山区粤海街道高新区高新南一道德赛大厦23层(2303-2306)",  "roleId": 0,  "address": null,  "createdAt": "2018-09-12T08:13:12.000Z",  "updatedAt": "2019-08-08T01:37:13.000Z"}
  1. 紧凑输出
jq -c . demo.json{"id":1,"username":"xiaoyao","email":"765485868@qq.com","cellphone":"18702759796","nickname":"逍遥","portrait":"https://statics.qiniu.wxaxiaoyao.cn/_/portraits/x1.png","sex":"男","motto":"世间有异贾, 专售荒唐梦, 以慰失意人, 闻者购如风. 莫问梦醒时, 图乐在梦中, 人生是何物? 百年一场梦.","description":"世间有异贾,专售荒唐梦,以慰失意人,闻者购如风。莫问梦醒时,图乐在梦中,人生是何物?百年一场梦.","location":"广东省深圳市南山区粤海街道高新区高新南一道德赛大厦23层(2303-2306)","roleId":0,"address":null,"createdAt":"2018-09-12T08:13:12.000Z","updatedAt":"2019-08-08T01:37:13.000Z"}
  1. 排序键
jq -S . demo.json{  "address": null,  "cellphone": "18702759796",  "createdAt": "2018-09-12T08:13:12.000Z",  "description": "世间有异贾,专售荒唐梦,以慰失意人,闻者购如风。莫问梦醒时,图乐在梦中,人生是何物?百年一场梦.",  "email": "765485868@qq.com",  "id": 1,  "location": "广东省深圳市南山区粤海街道高新区高新南一道德赛大厦23层(2303-2306)",  "motto": "世间有异贾, 专售荒唐梦, 以慰失意人, 闻者购如风. 莫问梦醒时, 图乐在梦中, 人生是何物? 百年一场梦.",  "nickname": "逍遥",  "portrait": "https://statics.qiniu.wxaxiaoyao.cn/_/portraits/x1.png",  "roleId": 0,  "sex": "男",  "updatedAt": "2019-08-08T01:37:13.000Z",  "username": "xiaoyao"}
  1. 过滤字段
jq '.motto' demo.json   # 单字段"世间有异贾, 专售荒唐梦, 以慰失意人, 闻者购如风. 莫问梦醒时, 图乐在梦中, 人生是何物? 百年一场梦."jq  '.nickname, .motto' demo.json  # 多字段, 用户逗号分隔"逍遥""世间有异贾, 专售荒唐梦, 以慰失意人, 闻者购如风. 莫问梦醒时, 图乐在梦中, 人生是何物? 百年一场梦."

更多过滤用法参考: man jq

转载于:https://www.cnblogs.com/--xiaoyao--/p/11378829.html

你可能感兴趣的文章
程序集的混淆及签名
查看>>
判断9X9数组是否是数独的java代码
查看>>
00-自测1. 打印沙漏
查看>>
UNITY在VS中调试
查看>>
SDUTOJ3754_黑白棋(纯模拟)
查看>>
Scala入门(1)Linux下Scala(2.12.1)安装
查看>>
如何改善下面的代码 领导说了很耗资源
查看>>
Quartus II 中常见Warning 原因及解决方法
查看>>
php中的isset和empty的用法区别
查看>>
Android ViewPager 动画效果
查看>>
pip和easy_install使用方式
查看>>
博弈论
查看>>
Redis sentinel & cluster 原理分析
查看>>
我的工作习惯小结
查看>>
把word文档中的所有图片导出
查看>>
浏览器的判断;
查看>>
ubuntu 18.04取消自动锁屏以及设置键盘快捷锁屏
查看>>
Leetcode 589. N-ary Tree Preorder Traversal
查看>>
机器学习/深度学习/其他开发环境搭建记录
查看>>
xml.exist() 实例演示
查看>>