1. 程式人生 > 其它 >jq - 一個靈活輕量的命令列JSON處理器

jq - 一個靈活輕量的命令列JSON處理器

jq 是一個輕量級和靈活的命令列 JSON 處理器。官方文件 https://stedolan.github.io/jq/tutorial/ 參考連結

jq是一個輕量級和靈活的命令列JSON處理器。官方文件 參考連結

安裝

# Mac 安裝 jq 指令
$ brew install jq

示例

# 原始資料
$ curl -s https://suggest.taobao.com/sug\?code\=utf-8\&q\=美圖秀秀
{"result":[["美圖秀秀vip","3193.8094555873927"]]}%

# json 格式化 pretty .
$ curl -s https://suggest.taobao.com/sug\?code\=utf-8\&q\=美圖秀秀 | jq .
{
  "result": [
    [
      "美圖秀秀vip",
      "4020.6379542395694"
    ]
  ]
}

# json 壓縮輸出 -c
$ curl -s https://suggest.taobao.com/sug\?code\=utf-8\&q\=美圖秀秀 | jq -c
{"result":[["美圖秀秀vip","4020.6379542395694"]]}

# 解析資料 .result[][0]
$ curl -s https://suggest.taobao.com/sug\?code\=utf-8\&q\=美圖秀秀 | jq '.result[][0]'
"美圖秀秀vip"

# 遍歷列印陣列 .[]
$ echo '[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]'|jq '.[]' -c
{"name":"lee","age":12}
{"name":"han","age":21}
{"name":"tom","age":50}

# | 通過管道將一個過濾器的輸出當做下一個過濾器的輸入
$ echo '[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]' | jq '.[] | .name'
"lee"
"han"
"tom"

# 按條件篩選 map(select(.age>20))
$ echo '[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]' | jq 'map(select(.age>20))' -c
[{"name":"han","age":21},{"name":"tom","age":50}]

類似指令

node 命令列 json 處理器 官方文件

# 安裝 nodejs 模組 json
$ npm install -g json

# 原始資料
$ curl -s https://suggest.taobao.com/sug\?code\=utf-8\&q\=美圖秀秀
{"result":[["美圖秀秀vip","3193.8094555873927"]]}

# 格式化 json
$ curl -s https://suggest.taobao.com/sug\?code\=utf-8\&q\=美圖秀秀 | json
{
  "result": [
    [
      "美圖秀秀vip",
      "3193.8094555873927"
    ]
  ]
}

# 解析資料
$ curl -s https://suggest.taobao.com/sug\?code\=utf-8\&q\=美圖秀秀 | json result | json -a 0
美圖秀秀vip

# 遍歷陣列 -a
$ echo '[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]'| json -a
{
  "name": "lee",
  "age": 12
}
{
  "name": "han",
  "age": 21
}
{
  "name": "tom",
  "age": 50
}

# 壓縮輸出
$ echo '[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]'|json -o jsony-0
[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]

# 按條件篩選 -c 'this.age > 20'
$ echo '[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]'| json -c 'this.age > 20'
[
  {
    "name": "han",
    "age": 21
  },
  {
    "name": "tom",
    "age": 50
  }
]

# 寫入檔案 
$ echo '[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]' > sample.json

# 讀取檔案 -f
$ json -f sample.json -a 0 name age
lee 12
han 21
tom 50

# 編輯
$ json -f sample.json -e 'this.gender=1'
[
  {
    "name": "lee",
    "age": 12,
    "gender": 1
  },
  {
    "name": "han",
    "age": 21,
    "gender": 1
  },
  {
    "name": "tom",
    "age": 50,
    "gender": 1
  }
]

# 取第一個
$ echo '[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]'|json 1 -o jsony-0
{"name":"han","age":21}

# 取最後一個
$ echo '[{"name":"lee","age":12},{"name":"han","age":21},{"name":"tom","age":50}]'|json -- -1
{
  "name": "tom",
  "age": 50
}


作者:Plucky
出處:https://www.1991.wiki/topics/2
版權:本作品採用「署名-非商業性使用-相同方式共享 4.0 國際」許可協議進行許可,如您轉載必須以連結形式註明原文地址。

滿招損,謙受益。