1

I have tho following JSON file

[
    {
        "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/"
    }
]

and I want to retrieve the base-uri depending on the event-type.
I am writing a bash script to do that. I've never worked with JSON files or wrote python codes before, so I am sorry if my question seems so naive. Can I do this in Bash or it would be easier if I used python?
Thank you

Fatma
  • 23
  • 5
  • Since you're using Python, why not use Python? https://docs.python.org/2/library/json.html – Jonathan M Feb 11 '15 at 22:07
  • `jq` is also widely recommended, amongst the many duplicates of this question. – Charles Duffy Feb 11 '15 at 22:21
  • @JonathanM, ...the only reason I'm not a fan of that question is that it _explicitly_ specifies `sed` and `awk`, rather than leaving the specification open for the best tool for the job, whatever that may be. (Granted, there are a great many answers that don't conform to that unfortunate specification). – Charles Duffy Feb 11 '15 at 22:23
  • @CharlesDuffy, yep, I'm not recommending the question, just pointing to the answer, which is just one way to skin the cat. – Jonathan M Feb 11 '15 at 22:27

1 Answers1

0

I use jshon, which I believe is available on ubuntu's repositories:

apt-get install jshon

I haven't tested this, but you will want to do something like:

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

which would print 1423675036

See here for more info: http://kmkeen.com/jshon/

boileau
  • 817
  • 6
  • 20