1

I have a list, called filelist = [] to which I'm appending a dictionary of some key value pairs.

filelist.append({"url": url, "exename": ename, "filename": fname}] as I loop through a set of files.
Later, loads() the below json object to a "json" key: 

for file in filelist: file["json"] = json.loads(json)

I am having no trouble pulling out the first layer of dictionary values, ie.

for files in filelist:
    print files["json"]["response_code"]

>>> 1

However, I am having issues at getting to the inner dictionary I'm assuming is bound to files["json"]["scans"] for example:

for files in filelist:
    for avvendors in files["json"]["scans"]

I'm really interested in pulling out the "detected" key but it seems python is not loading the "scans" key as a dictionary but instead is storing it as a string. I'm thinking of doing a second loads() on the inner dictionary to see what happens.

for stuff in avvendors:
    if stuf["detected"]:
        #do something

I'm not really sure what is happening here. I know I will likely figure it out but some insight into what python does with the json internally would be nice. Also, I will not get a chance to work on this till Monday or Tuesday, maybe even Wednesday and the suspense is killing me.

Here's the full block for readability:

for files in filelist: for avvendors in files["json"]["scans]: if avvendors["detected"]: #Do something useful

{"response_code": 1,
"verbose_msg": "Scan finished, scan information embedded in this object",

"resource": "99017f6eebbac24f351415dd410d522d",
"scan_id": "52d3df0ed60c46f336c131bf2ca454f73bafdc4b04dfa2aea80746f5ba9e6d1c-1273894724",
"md5": "99017f6eebbac24f351415dd410d522d",
"sha1": "4d1740485713a2ab3a4f5822a01f645fe8387f92",
"sha256": "52d3df0ed60c46f336c131bf2ca454f73bafdc4b04dfa2aea80746f5ba9e6d1c",

"scan_date": "2010-05-15 03:38:44",

"positives": 40,
"total": 40,
"scans": {"nProtect": {"detected": true, "version": "2010-05-14.01", "result": "Trojan.Generic.3611249", "update": "20100514"},
"CAT-QuickHeal": {"detected": true, "version": "10.00", "result": "Trojan.VB.acgy", "update": "20100514"},
"McAfee": {"detected": true, "version": "5.400.0.1158", "result": "Generic.dx!rkx", "update": "20100515"},
"TheHacker": {"detected": true, "version": "6.5.2.0.280", "result": "Trojan/VB.gen", "update": "20100514"},
"VirusBuster": {"detected": true, "version": "5.0.27.0", "result": "Trojan.VB.JFDE", "update": "20100514"},
"NOD32": {"detected": true, "version": "5115", "result": "a variant of Win32/Qhost.NTY", "update": "20100514"},
"F-Prot": {"detected": false, "version": "4.5.1.85", "result": null, "update": "20100514"},
"Symantec": {"detected": true, "version": "20101.1.0.89", "result": "Trojan.KillAV", "update": "20100515"},
"Norman": {"detected": true, "version": "6.04.12", "result": "W32/Smalltroj.YFHZ", "update": "20100514"},
"TrendMicro-HouseCall": {"detected": true, "version": "9.120.0.1004", "result": "TROJ_VB.JVJ", "update": "20100515"},
"Avast": {"detected": true, "version": "4.8.1351.0", "result": "Win32:Malware-gen", "update": "20100514"},
"eSafe": {"detected": true, "version": "7.0.17.0", "result": "Win32.TRVB.Acgy", "update": "20100513"},
"ClamAV": {"detected": false, "version": "0.96.0.3-git", "result": null, "update": "20100514"},
"Kaspersky": {"detected": true, "version": "7.0.0.125", "result": "Trojan.Win32.VB.acgy", "update": "20100515"},
"BitDefender": {"detected": true, "version": "7.2", "result": "Trojan.Generic.3611249", "update": "20100515"},
"Comodo": {"detected": true, "version": "4842", "result": "Heur.Suspicious", "update": "20100515"},
"F-Secure": {"detected": true, "version": "9.0.15370.0", "result": "Trojan.Generic.3611249", "update": "20100514"},
"DrWeb": {"detected": true, "version": "5.0.2.03300", "result": "Trojan.Hosts.37", "update": "20100515"},
"AntiVir": {"detected": true, "version": "8.2.1.242", "result": "TR/VB.acgy.1", "update": "20100514"},
"TrendMicro": {"detected": true, "version": "9.120.0.1004", "result": "TROJ_VB.JVJ", "update": "20100514"},
"McAfee-GW-Edition": {"detected": true, "version": "2010.1", "result": "Generic.dx!rkx", "update": "20100515"},
"Sophos": {"detected": true, "version": "4.53.0", "result": "Troj/VBHost-A", "update": "20100515"},
"eTrust-Vet": {"detected": true, "version": "35.2.7490", "result": "Win32/ASuspect.HDBBD", "update": "20100515"},
"Authentium": {"detected": false, "version": "5.2.0.5", "result": null, "update": "20100514"},
"Jiangmin": {"detected": true, "version": "13.0.900", "result": "Trojan/VB.yqh", "update": "20100514"}, [...] }
0

1 Answer 1

2

Be careful when iterating over a dict. It just returns the keys. If you want the keys and values, use the .iteritems() method (or in Python3, .items()):

for files in filelist:
   for avvendor, stuf in files["json"]["scans"].iteritems():
       if stuf["detected"]:
          #do something

In your case, the value of the scans key is a dict: "scans": {"nProtect": ...}

Sign up to request clarification or add additional context in comments.

4 Comments

Remember than in python 3.x this is just dict.items().
So python is probably loading the inner dictionary as a dictionary and I'm wrong in assuming that it was just loaded as a string?
json.loads() returns a Python object, such as a list of dicts of dicts. So yes, the inner dictionary is a dict, not a string.
nevermind, now that I recall, "true" was getting changed to True and "AntiVir" was changing to u'AntiVir' etc. when used print on the whole mess provided, Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.