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