Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filter and print particular json dictionaries in python

I’m in the process of learning python. I encountered a problem with json that I can’t overcome.

I have this dataset from json in python:

{
    "Sophos": {
        "detected": true,
        "result": "phishing site"
    },
    "Phishtank": {
        "detected": false,
        "result": "clean site"
    },
    "CyberCrime": {
        "detected": false,
        "result": "clean site"
    },
    "Spam404": {
        "detected": false,
        "result": "clean site"
    },
    "SecureBrain": {
        "detected": false,
        "result": "clean site"
    },
    "Hoplite Industries": {
        "detected": false,
        "result": "clean site"
    },
    "CRDF": {
        "detected": false,
        "result": "clean site"
    },
    "Rising": {
        "detected": false,
        "result": "clean site"
    },
    "Fortinet": {
        "detected": true,
        "result": "phishing site"
    },
    "alphaMountain.ai": {
        "detected": true,
        "result": "phishing site"
    },
    "Lionic": {
        "detected": false,
        "result": "clean site"
    },
    "Cyble": {
        "detected": false,
        "result": "clean site"
    }
}

I would like to filter these dictionaries in such a way as to print only those keys and values in which "detected": true.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For example I would like print only

{
    "Sophos": {
        "detected": true,
        "result": "phishing site"
    },
    "Fortinet": {
        "detected": true,
        "result": "phishing site"
    }
}

I use VirusTotal apikey v2 https://developers.virustotal.com/v2.0/reference/domain-report
My code in python:

parameters = {'apikey': api_key, 'resource': domain}

response = requests.get(url, params=parameters)
    
python_response = json.loads(response.text)

scans = python_response["scans"]

example = json.dumps(python_response["scans"], indent=4)

print(example)

I’m looking for a simple and readable way to do it so that I understand it as best I can. I would like print result in Python. I searched and read various solutions for this (list comprehension or filter() with lambda), but it did not help me.

I’m still learning, thanks in advance for your understanding if it’s a simple case.

Thank you in advance for your help and answers.

>Solution :

You can use dict comprehension to filter the response dictionary.
Note that in the example you provided, I think you have json data and not the python object. true is not a valid boolean keyword in python, it should’ve been True instead.

filtered = {k: v for k, v in orignal_dict.items() if v.get("detected") == true}

For your example –

true = True
false = False

data = {
    "Sophos": {
        "detected": true,
        "result": "phishing site"
    },
    "Phishtank": {
        "detected": false,
        "result": "clean site"
    },
    "CyberCrime": {
        "detected": false,
        "result": "clean site"
    },
    "Spam404": {
        "detected": false,
        "result": "clean site"
    },
    "SecureBrain": {
        "detected": false,
        "result": "clean site"
    },
    "Hoplite Industries": {
        "detected": false,
        "result": "clean site"
    },
    "CRDF": {
        "detected": false,
        "result": "clean site"
    },
    "Rising": {
        "detected": false,
        "result": "clean site"
    },
    "Fortinet": {
        "detected": true,
        "result": "phishing site"
    },
    "alphaMountain.ai": {
        "detected": true,
        "result": "phishing site"
    },
    "Lionic": {
        "detected": false,
        "result": "clean site"
    },
    "Cyble": {
        "detected": false,
        "result": "clean site"
    }
}


filtered = {k: v for k, v in data.items() if v.get("detected") == true}
print(json.dumps(filtered, indent=4))

Output:

{
    "Sophos": {
        "detected": true,
        "result": "phishing site"
    },
    "Fortinet": {
        "detected": true,
        "result": "phishing site"
    },
    "alphaMountain.ai": {
        "detected": true,
        "result": "phishing site"
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading