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
]

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

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值