raghad
raghad

Reputation: 25

How to print list of strings in dict in JSON

Hello I tried to print the dll_loaded list

"behavior": {
    "generic": [
        {
            "process_path": "C:\\Windows\\System32\\wscript.exe", 
            "summary": {
                "dll_loaded": [
                    "C:\\Windows\\system32\\wshext.dll", 
                    "C:\\Windows\\system32\\advapi32.dll", 
                ]
             }
         }

but it always give me this error TypeError: string indices must be integers

I tried to print it using the following

        for sys in json_data["behavior"]["generic"]:
            for sys1 in sys["summary"]:
                for sys2 in sys1["dll_loaded"]:
                    print(sys2)

I tried to print the type of sys1 and it gives me <class 'str'>, shouldn't be a list? then tried to treat sys1 as a string and print using range and len but that only returned the characters one by one!

but I am not sure where I am going wrong, could someone help please? I am using Python 3.7

EDIT
I tried John P answer and it worked but now I am facing another problem such as dll_loaded is not the first element in the list

"behavior": {
    "generic": [
        {
            "process_path": "C:\\Windows\\System32\\wscript.exe", 
            "summary": {
                "file_created": [
                    "C:\\Users\\Administrator\\AppData\\Roaming\\WinRAR\\version.dat"
                ], 
                "file_recreated": [
                    "\\Device\\DfsClient"
                ], 
                "directory_created": [
                    "C:\\Users\\Administrator\\AppData\\Roaming\\WinRAR"
                ], 
                "dll_loaded": [
                    "C:\\Windows\\system32\\wshext.dll", 
                    "C:\\Windows\\system32\\advapi32.dll", 
                ]
             }
         }

Sorry but I am still a beginner at Python

Upvotes: 0

Views: 66

Answers (2)

Lilith Schneider
Lilith Schneider

Reputation: 51

I think you just read the data structure wrong. There was no list under "summary", so you had one too many loops.

for sys in json_data["behavior"]["generic"]:
    for sys1 in sys["summary"].get("dll_loaded", []):
        print(sys1)

Upvotes: 0

John
John

Reputation: 26

I think what you are looking for is something like this:

for sys in json_data["behavior"]["generic"][0]["summary"]['dll_loaded']:
    print(sys)

It is unnecessary to have 3 layers of for-loop since you are only interested in the list generated from json_data["behavior"]["generic"][0]["summary"]['dll_loaded'].

With regards to your problem of why the type of sys1 is <class 'str'>, this is because sys["summary"] gives a one-element dictionary instead of a list. And when iterating through a dictionary in the case of for sys1 in sys["summary"], sys1 refers to the keys in the dictionary, in which case only refers to the string "dll_loaded".

If you insist on having nested for-loops (not advised), the correct way who be something like this:

for sys in json_data["behavior"]["generic"]:
   for sys1 in sys["summary"]:
      for sys2 in sys["summary"][sys1]:
         print(sys2)

Upvotes: 1

Related Questions