Eduardo Reis
Eduardo Reis

Reputation: 13

Comparing Json data. Python 3

I have the following Json file and I need to compare data to see how many times each value repeat itself. The problem is, I have no idea about handling Json. I don't want the answer to my exercise, I want to know how to access the data. Json:

{
    "tickets": [
        {
            "ticket_id": 0,
            "timestamp": "2016/05/26 04:47:02",
            "file_hash": "c9d4e03c5632416f",
            "src_ip": "6.19.128.119",
            "dst_ip": "145.231.76.44"
        },
        {
            "ticket_id": 1,
            "timestamp": "2017/05/28 16:14:22",
            "file_hash": "ce8a056490a3fd3c",
            "src_ip": "100.139.125.30",
            "dst_ip": "145.231.76.44"
        },
        {
            "ticket_id": 2,
            "timestamp": "2015/08/23 03:27:10",
            "file_hash": "d17f572496f48a11",
            "src_ip": "67.153.41.75",
            "dst_ip": "239.168.56.243"
        },
        {
            "ticket_id": 3,
            "timestamp": "2016/02/26 14:01:33",
            "file_hash": "3b28f2abc966a386",
            "src_ip": "6.19.128.119",
            "dst_ip": "137.164.166.84"
        },
                ]
}

Upvotes: 0

Views: 79

Answers (2)

LAZ
LAZ

Reputation: 534

If this is a string representation of the object, first you need to set a variable and parse the string to have object you can work with.

jsonString = "{...your json string...}"

Then parse the string,

import json

jsonObject = json.loads(jsonString)

To access the data within it's like any other js object. Example :

jsonObject.tickets[0].timestamp would return "2016/05/26 04:47:02"

tickets is the key within the jsonObject, 0 is the index of the first object in the list of tickets.

Upvotes: 1

paulsm4
paulsm4

Reputation: 121649

  1. You can use the built-in "json" library to parse your file into an object:

    import json
    f = open('myfile.json','r')
    tickets = json.loads(f.read())
    
  2. This will return a "tickets" object. How you "compare" (or what exactly you compare) is up to you.

Upvotes: 0

Related Questions