0

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

raghad
  • 25
  • 8
  • Please provide the entire error message, as well as a [mcve]. What do/don’t you understand from that error message? – AMC Mar 07 '20 at 03:49
  • Does this answer your question? [Why am I seeing "TypeError: string indices must be integers"?](https://stackoverflow.com/questions/6077675/why-am-i-seeing-typeerror-string-indices-must-be-integers) – AMC Mar 07 '20 at 03:49
  • I didn't understand how to access that part of the JSON file but it seems I misinterpreted the structure. And the question is already answered by John P, thanks – raghad Mar 07 '20 at 16:24

2 Answers2

1

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)
John
  • 26
  • 4
  • it worked thank you for answering and clarifying the type of sys1! but what does [0] represent? – raghad Mar 07 '20 at 02:54
  • [0] means the first element of a list. json_data["behavior"]["generic"] is a list with one element (the one element is a dictionary). So to get the dictionary, we need [0]. – John Mar 07 '20 at 03:31
  • what if the dll_loaded is not the first one in the list and summary consist of the following how would I iterate through it? like this "summary": { "file_created": [], "file_recreated": [], "directory_created": [], "dll_loaded": [] } – raghad Mar 07 '20 at 23:39
  • could you please help me with the other problem? – raghad Mar 08 '20 at 01:36
0

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)
  • it gives me `KeyError: 'dll_loaded'` I have edited the question and the edit shows that if I used this approach it will generate this error. – raghad Mar 08 '20 at 01:05
  • oh, if one or more of the keys in the dictionary is optional (may or may not exist), then you can use `dict.get(key, defaultvalue)` I will edit my answer to demonstrate. [post about .get()](https://stackoverflow.com/questions/11041405/why-dict-getkey-instead-of-dictkey) – Lilith Schneider Mar 09 '20 at 18:48