Python Forum
Need help Understanding JSON
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help Understanding JSON
#1
Hello, I am not very good with python currently. I am trying to pull data from a json file. I have found seeral examples but nothing seems to work for me. The examples seem to work with the example file. When I try to use my file I get an error "list indices must be integers or slices, not dict". For example this works as expected https://pythonguides.com/json-data-in-python/. When I change the file name to load my file it throws that error. This is very frustrating. There are a lot of these sections. If anyone can point me to an article or can explain how to do what I want. Which is just extract all the urls out of the file. Thank you.

Los of these sections
"kind": "some text"
"Entries": [

This is a small example of the json file I am trying to manipulate:

[
    {
        "kind": "Symantec Endpoint Protection client/ Symantec Agent",
        "entries": [
            {
                "date": "2021-06-14",
                "url": "https://liveupdate.symantec.com",
                "description": "Agent Installation Package",
                "inbound": false,
                "outbound": true
            },
            {
                "date": "2021-06-14",
                "url": "https://liveupdate.symantecliveupdate.com",
                "description": "Agent Installation Package",
                "inbound": false,
                "outbound": true
            },
            {
                "date": "2021-06-14",
                "url": "https://ent-shasta-rrs.symantec.com",
                "description": "Symantec reputation servers",
                "inbound": false,
                "outbound": true
            },
             "outbound": true
            }
        ]
    },
    {
        "kind": "Additional Entries for Cloud Managed/ Hybrid managed Agents",
        "entries": [
            {
                "date": "2021-06-14",
                "url": "https://usea1.r3.securitycloud.symantec.com",
                "description": "Symantec Cloud API gateway",
                "inbound": false,
                "outbound": true
            },
            {
                "date": "2021-06-14",
                "url": "https://us.spoc.securitycloud.symantec.com",
                "description": "Cloud notification service (SPOC)",
                "inbound": false,
                "outbound": true
            },
            {
                "date": "2021-06-14",
                "url": "https://storage.googleapis.com",
                "description": "Cloud storage services, Live Shell",
                "inbound": false,
                "outbound": true
            },
            {
                "date": "2021-06-14",
                "url": "https://ws.securitycloud.symantec.com",
                "description": "Live Shell",
                "inbound": false,
                "outbound": true
            },
            {
                "date": "2021-06-14",
                "url": "https://bds.securitycloud.symantec.com",
                "description": "Live Shell",
                "inbound": false,
                "outbound": true
            }
        ]
    },
Gribouillis write Aug-15-2022, 02:35 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Please post your code and the entire error message. It is impossible to know what your error is if we cannot see the code that generates the error.
If you do something like this:
data = json.load(json_filename)
data will be a list of dictionaries. Each of these dictionaries has keys for "kind" and "entries". data[index].entries is another list of dictionaries. Each of these dictionaries has keys "date", "url", "description", "inbound", "outbound".

I wrote a little program to load your json as a string (use loads instead of load). There is are errors in the json you posted. These must be cut and paste errors, otherwise you would be getting JSONDecoderErrors.
import json

json_str = """
[
{
"kind": "Symantec Endpoint Protection client/ Symantec Agent",
"entries": [
{
"date": "2021-06-14",
"url": "https://liveupdate.symantec.com",
"description": "Agent Installation Package",
"inbound": false,
"outbound": true
},
{
"date": "2021-06-14",
"url": "https://liveupdate.symantecliveupdate.com",
"description": "Agent Installation Package",
"inbound": false,
"outbound": true
},
{
"date": "2021-06-14",
"url": "https://ent-shasta-rrs.symantec.com",
"description": "Symantec reputation servers",
"inbound": false,
"outbound": true
}
]
},
{
"kind": "Additional Entries for Cloud Managed/ Hybrid managed Agents",
"entries": [
{
"date": "2021-06-14",
"url": "https://usea1.r3.securitycloud.symantec.com",
"description": "Symantec Cloud API gateway",
"inbound": false,
"outbound": true
},
{
"date": "2021-06-14",
"url": "https://us.spoc.securitycloud.symantec.com",
"description": "Cloud notification service (SPOC)",
"inbound": false,
"outbound": true
},
{
"date": "2021-06-14",
"url": "https://storage.googleapis.com",
"description": "Cloud storage services, Live Shell",
"inbound": false,
"outbound": true
},
{
"date": "2021-06-14",
"url": "https://ws.securitycloud.symantec.com",
"description": "Live Shell",
"inbound": false,
"outbound": true
},
{
"date": "2021-06-14",
"url": "https://bds.securitycloud.symantec.com",
"description": "Live Shell",
"inbound": false,
"outbound": true
}
]
}
]
"""

data = json.loads(json_str)
print(type(data), len(data))
print(type(data[0]), list(data[0].keys()))
print(type(data[0]["kind"]), type(data[0]["entries"]))
print(type(data[0]["entries"][0]), list(data[0]["entries"][0].keys()))
print(data[0]["entries"][0])
print(type(data[0]["entries"][0]["outbound"]), data[0]["entries"][0]["outbound"])
Output:
<class 'list'> 2 <class 'dict'> ['kind', 'entries'] <class 'str'> <class 'list'> <class 'dict'> ['date', 'url', 'description', 'inbound', 'outbound'] {'date': '2021-06-14', 'url': 'https://liveupdate.symantec.com', 'description': 'Agent Installation Package', 'inbound': False, 'outbound': True} <class 'bool'> True
Reply
#3
I do not work with JSON at all. I just noticed something about the format of it. It starts with [

{
"kind": "Symantec Endpoint Protection client/ Symantec Agent",
"entries": [

All the examples I see there is only one key. In this instance would I need to use something like kind.entries?
Reply
#4
How a json file starts depends on what you write to the file.

json.dump([1, 2, 3, 4], file) writes a file that contains [1, 2, 3, 4].

json.dump({"one":1, "two":2, "three":3}, file) writes a file that contains {"one": 1, "two": 2, "three": 3}.

json.dump("hello", file) writes a file that contains "hello".

json.dump(42, file) writes a file that contains 42.

Look at my earlier post. It shows how to drill down and look at the object created by json.load() or json.loads().

Try running this code. Maybe it will help you understand.
import json

# Replace with name of your json file
with open("data.txt", "r") as f:
    data = json.load(f)

# data is a list, because the json file starts with "[" and ends with "]".
# We can index a list or loop through a list.  Here we loop.
for thing in data:
    # thing is a dictionary.  You can tell this because it starts with "{" and ends with "}".
    # It has keys "kind" and "entries".
    print(thing["kind"])
    for entry in thing["entries"]:
        # thing["entries"] is a list.
        for key in entry:
            # entry is a dictionary.  Here we loop through the dictionary keys which we use
            # to pretty print the dictionary values.
            print(f"    {key.title():20} {entry[key]}")
        print()
    print()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020