JSON 命令行工具

原文連接: https://wxaxiaoyao.cn/article/92git

JQ

jq 是一個處理 JSON 數據的 LINUX 命令行工具.github

命令幫助

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

相關文章
相關標籤/搜索