在bash中解析JSON文件

时间:2015-02-11 22:01:53

标签: python json bash

我有以下JSON文件

[
    {
        "bw-parallel-streams": "1",
        "destination": "198.124",
        "event-types": [
            {
                "base-uri": "/esmond/perfsonar/archive/dfbd05cef48c4111b9da3cc1238f74ad/throughput-subintervals/base",
                "event-type": "throughput-subintervals",
                "summaries": [],
                "time-updated": 1423675036
            },
            {
                "base-uri": "/esmond/perfsonar/archive/dfbd05cef48c4111b9da3cc1238f74ad/failures/base",
                "event-type": "failures",
                "summaries": [],
                "time-updated": null
            },
            {
                "base-uri": "/esmond/perfsonar/archive/dfbd05cef48c4111b9da3cc1238f74ad/packet-retransmits/base",
                "event-type": "packet-retransmits",
                "summaries": [],
                "time-updated": 1423675036
            },
            {
                "base-uri": "/esmond/perfsonar/archive/dfbd05cef48c4111b9da3cc1238f74ad/throughput/base",
                "event-type": "throughput",
                "summaries": [
                    {
                        "summary-type": "average",
                        "summary-window": "86400",
                        "time-updated": 1423675036,
                        "uri": "/esmond/perfsonar/archive/dfbd05cef48c4111b9da3cc1238f74ad/throughput/averages/86400"
                    }
                ],
                "time-updated": 1423675036
            },
            {
                "base-uri": "/esmond/perfsonar/archive/dfbd05cef48c4111b9da3cc1238f74ad/packet-retransmits-subintervals/base",
                "event-type": "packet-retransmits-subintervals",
                "summaries": [],
                "time-updated": 1423675036
            }
        ],
        "input-destination": "wash-pt1.es.net",
        "input-source": "128.143",
        "ip-transport-protocol": "tcp",
        "measurement-agent": "128.143.231.161",
        "metadata-count-total": 1,
        "metadata-key": "dfbd05cef48c4111b9da3cc1238f74ad",
        "source": "128.143.",
        "subject-type": "point-to-point",
        "time-duration": "20",
        "time-interval": "21600",
        "tool-name": "bwctl/iperf3",
        "uri": "/esmond/perfsonar/archive/dfbd05cef48c4111b9da3cc1238f74ad/"
    }
]

我希望根据事件类型检索base-uri 我正在写一个bash脚本来做到这一点。我以前从未使用过JSON文件或编写过python代码,所以如果我的问题看起来很天真,我很抱歉。我可以在Bash中执行此操作,或者如果我使用python会更容易吗? 谢谢

1 个答案:

答案 0 :(得分:0)

我使用的是jshon,我认为它可以在ubuntu的存储库中找到:

apt-get install jshon

我还没有对此进行测试,但你会想做类似的事情:

jshon -e 0 -e event-types -e 0 -e time-updated -u < data.json

将打印1423675036

有关详细信息,请参阅此处:http://kmkeen.com/jshon/