LINUX SHELL 解析JSON格式

LINUX SHELL 解析JSON格式

1. 使用工具jq

https://stedolan.github.io/jq/

下载版本linux版本

jq-linux64

Ubuntu 直接sudo apt-get install jq即可。

2. 解析json

[root@cas script]# cat test.json 
{"tasks":[{"id":"simpletour-wechat.a17506e7-8204-11e6-a750-da3323f1fcbf","slaveId":"09aef570-d348-47e6-a03b-08c8658d4346-S2","host":"192.168.2.149","startedAt":"2016-09-24T03:12:01.836Z","stagedAt":"2016-09-24T03:11:48.168Z","ports":[31872],"version":"2016-09-24T03:11:47.971Z","ipAddresses":[{"ipAddress":"172.17.0.2","protocol":"IPv4"}],"appId":"/simpletour-wechat"}]}

[root@cas script]# cat test.json | jq
{
  "tasks": [
    {
      "id": "simpletour-wechat.a17506e7-8204-11e6-a750-da3323f1fcbf",
      "slaveId": "09aef570-d348-47e6-a03b-08c8658d4346-S2",
      "host": "192.168.2.149",
      "startedAt": "2016-09-24T03:12:01.836Z",
      "stagedAt": "2016-09-24T03:11:48.168Z",
      "ports": [
        31872
      ],
      "version": "2016-09-24T03:11:47.971Z",
      "ipAddresses": [
        {
          "ipAddress": "172.17.0.2",
          "protocol": "IPv4"
        }
      ],
      "appId": "/simpletour-wechat"
    }
  ]
}
[root@cas script]#

3. 基本用法:

显示key 的value

[root@cas script]# cat test.json | jq ".tasks"

4. 高级用法

[root@cas script]# cat test.json | jq '.tasks[] | {host,ports}'
{
  "host": "192.168.2.149",
  "ports": [
    31872
  ]
}
[root@cas script]# 

jq手册参考地址:https://stedolan.github.io/jq/manual/#Advancedfeatures

相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页