sudo-potato
sudo-potato

Reputation: 171

Printing Specific Value from Multi Nested Dict

(Very new to Python) I have a multi-nested dict in JSON format and I'm trying to check if a specific key value or true or false. I'm unsure of the syntax to check for a key nested within several dicts. I am using the output of URLVoid's API which looks something like this:

"data": {
    "report": {
        "dns_records": {
            "ns": {
                "records": [
                    {
                        "target": "alexis.ns.cloudflare.com",
                        "ip": "x.x.x.x",
                        "country_code": "US",
                        "country_name": "United States of America",
                        "isp": "CloudFlare Inc."
                    },
                    {
                        "target": "vida.ns.cloudflare.com",
                        "ip": "x.x.x.x",
                        "country_code": "JP",
                        "country_name": "Japan",
                        "isp": "CloudFlare Inc."
                    }
                ]
            },
            "mx": {
                "records": []
            }
        },
        "domain_blacklist": {
            "engines": [
                {
                    "name": "SpamhausDBL",
                    "reference": "https://www.spamhaus.org/lookup/",
                    "detected": false
                },
                {
                    "name": "ThreatLog",
                    "reference": "https://www.threatlog.com/",
                    "detected": false
                },
                {
                    "name": "OpenPhish",
                    "reference": "https://www.openphish.com/",
                    "detected": false
                },
                {
                    "name": "PhishTank",
                    "reference": "https://www.phishtank.com/",
                    "detected": false
                },
                {
                    "name": "Phishing.Database",
                    "reference": "https://github.com/mitchellkrogza/Phishing.Database",
                    "detected": false
                },
                {
                    "name": "PhishStats",
                    "reference": "https://phishstats.info/",
                    "detected": false
                },
                {
                    "name": "URLVir",
                    "reference": "https://www.urlvir.com/",
                    "detected": false
                },
                {
                    "name": "URLhaus",
                    "reference": "https://urlhaus.abuse.ch/",
                    "detected": false
                },
                {
                    "name": "RPiList Not Serious",
                    "reference": "https://github.com/RPiList/specials",
                    "detected": false
                },
                {
                    "name": "precisionsec",
                    "reference": "https://precisionsec.com/",
                    "detected": false
                },
                {
                    "name": "AntiSocial Blacklist",
                    "reference": "https://theantisocialengineer.com/",
                    "detected": false
                },
                {
                    "name": "PhishFeed",
                    "reference": "https://phishfeed.com/",
                    "detected": false
                },
                {
                    "name": "Spam404",
                    "reference": "https://www.spam404.com/",
                    "detected": false
                },
                {
                    "name": "CRDF",
                    "reference": "https://threatcenter.crdf.fr/check.html",
                    "detected": true
                },
                {
                    "name": "Artists Against 419",
                    "reference": "http://wiki.aa419.org/index.php/Main_Page",
                    "detected": false
                },
                {
                    "name": "CERT Polska",
                    "reference": "https://www.cert.pl/",
                    "detected": false
                }
            ],
            "detections": 1

To test, I have the JSON saved in my working directory. I try

f = open("results.json", "r")
#print(f.read())
print(f.read()["data"]["reports"]["domain_blacklist"]["engines"][0])

Error:

Traceback (most recent call last):
  File "directory/path", line 19, in <module>
    print(f.read()["data"]["reports"]["domain_blacklist"]["engines"][0])
TypeError: string indices must be integers

What is the proper syntax to go through the data and check if the value of detected is true? I understand for a simple dict like: a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'} I can simply do

for key in dict:
   print key

But because this is multi-nested I am getting lost.

Upvotes: 1

Views: 55

Answers (1)

Vishal Singh
Vishal Singh

Reputation: 6234

After reading your JSON file you need json.load(fp) to deserialize fp (text object or binary file containing a JSON document) to a Python object.

import json

with open("results.json") as fp:
    json_content = json.load(fp)

for engine in json_content["data"]["reports"]["domain_blacklist"]["engines"]:
    print(engine["detected"])

Upvotes: 2

Related Questions