原文链接:
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"}
- 缩进输出
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"}
- 紧凑输出
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"}
- 排序键
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"}
- 过滤字段
jq '.motto' demo.json # 单字段"世间有异贾, 专售荒唐梦, 以慰失意人, 闻者购如风. 莫问梦醒时, 图乐在梦中, 人生是何物? 百年一场梦."jq '.nickname, .motto' demo.json # 多字段, 用户逗号分隔"逍遥""世间有异贾, 专售荒唐梦, 以慰失意人, 闻者购如风. 莫问梦醒时, 图乐在梦中, 人生是何物? 百年一场梦."
更多过滤用法参考: man jq