LINUX SHELL 解析JSON格式__LINUX

來源:互聯網
上載者:User
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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.