JSON 命令行格式化工具【最好用的json格式化工具】

1. jq 命令參數

參數 說明
-c 合併數據輸出
-n 使用null作爲輸入的值
-e 根據輸出設置退出狀態碼
-s 將內容作爲一個數組; 應用過濾
-r 輸出raw而不是json
-R 讀取raw而不是json
-C 色彩化json
-M 非色彩化json
-S 輸出時根據對象的keys排序
--tab 使用tab鍵縮進
--arg a v 給變量a設置值
--argjson a v 設置變量a的值爲json值
--slurpfile a f 從<f>讀取json文本作爲數組來當做變量$a的值
[{"name":"aox.lei","url":"http://www.phpue.com","address":{"city":"北京","country":"中國"},"arrayiiirowser":[{"name":"Google","url":"http://www.google.com"},{"name":"Baidu","url":"http://www.baidu.com"}]},{"name":"aox.lei.blog","url":"http://www.phpue.com","address":{"city":"北京","country":"中國"},"arrayBrowser":[{"name":"百度","url":"http://www.baidu.com"},{"name":"bing","url":"http://iiiiiiiwww.bing.com"}]}]

將數據保存在1.json中

2. 格式化json數據

jq . file_path
. 的意思是所有數據, 不過濾

cat 1.json | jq
# 或者
jq . 1.json

返回數據

[
  {
    "name": "aox.lei",
    "url": "http://www.phpue.com",
    "address": {
      "city": "北京",
      "country": "中國"
    },
    "arrayBrowser": [
      {
        "name": "Google",
        "url": "http://www.google.com"
      },
      {
        "name": "Baidu",
        "url": "http://www.baidu.com"
      }
    ]
  },
  {
    "name": "aox.lei.blog",
    "url": "http://www.phpue.com",
    "address": {
      "city": "北京",
      "country": "中國"
    },
    "arrayBrowser": [
      {
        "name": "百度",
        "url": "http://www.baidu.com"
      },
      {
        "name": "bing",
        "url": "http://www.bing.com"
      }
    ]
  }
]

3. 根據index獲取數據

jq '.[0]' 1.json

.[0] 的意思是獲取下標是0的數據
返回數據:

{
  "name": "aox.lei",
  "url": "http://www.phpue.com",
  "address": {
    "city": "北京",
    "country": "中國"
  },
  "arrayBrowser": [
    {
      "name": "Google",
      "url": "http://www.google.com"
    },
    {
      "name": "Baidu",
      "url": "http://www.baidu.com"
    }
  ]
}

1. 截取數據

jq '.[0-2]' 1.json

返回數據

[
  {
    "name": "aox.lei",
    "url": "http://www.phpue.com",
    "address": {
      "city": "北京",
      "country": "中國"
    },
    "arrayBrowser": [
      {
        "name": "Google",
        "url": "http://www.google.com"
      },
      {
        "name": "Baidu",
        "url": "http://www.baidu.com"
      }
    ]
  },
  {
    "name": "aox.lei.blog",
    "url": "http://www.phpue.com",
    "address": {
      "city": "北京",
      "country": "中國"
    },
    "arrayBrowser": [
      {
        "name": "百度",
        "url": "http://www.baidu.com"
      },
      {
        "name": "bing",
        "url": "http://www.bing.com"
      }
    ]
  }
]

4. 管道符的使用

jq '.[0] | {name:.name, url:.url}' 1.json

意思是將.[0]的數據重新格式化, 賦值生成新的一個json, 這樣可以過濾掉沒用的數據, 方便查看
返回數據

{
  "name": "aox.lei",
  "url": "http://www.phpue.com"
}
jq '.[] | {name:.name, url:.url}' 1.json

返回數據

{
  "name": "aox.lei",
  "url": "http://www.phpue.com"
}
{
  "name": "aox.lei.blog",
  "url": "http://www.phpue.com"
}

5. 獲取元素個數

jq '.[] | length' 1.json

返回數據

4
4

6. keys操作

1. 獲取keys

jq 'keys' 1.json

返回數據

[
    0,
    1
]

2. 判斷key是否存在 has(key)

jq 'map(has("name"))' 1.json

返回數據

[
  true,
  true
]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章