Python Forum

Full Version: string indices must be integers when parsing Json
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi. I have the json file and its content is like in the below.
I have a code for read and parse it. You can see my code in the below.


"['[{\"Manufacturer\": \"VMware, Inc.\", \"Model\": \"VMware7,1\", \"Name\": \"DC01\"}]', '[{\"Index\": \"1\", \"IPAddress\": [\"192.168.1.240,fe80::350e:d28d:14a5:5cbb\"]}]'


with open('data2.json', 'w') as jsonFile:
    json.dump(str(output_array), jsonFile)
    jsonFile.close()


mySql_insert_query = """INSERT INTO wmi_inventory (wmi_data, date) 
                                VALUES (%s, %s) """
now = datetime.now()
dt_string = now.strftime("%Y-%m-%d-%H:%M:%S")
record = (data, dt_string)
cursor.execute(mySql_insert_query, record)
connection.commit()

with open('data2.json','r') as lst:
     data=lst.read()
     d = json.loads(json.dumps(data))
     print(d["Name"])
When i run my code, im getting this error. How can i solve this?


Traceback (most recent call last):
  File "wmi_deneme.py", line 121, in <module>
    print(d["Name"])
TypeError: string indices must be integers
Please Always post enough code to run your example, and data to support it.
When posting Tracebacks (errors) post the entire unaltered error traceback. It contains valuable important information that leads up to the occurrence or the error.
Well well, @ilknurg , you are making a mess of it. Let us clean up. You are posting a MySql block, but that has nothing to do with your error. But first:
Output:
"['[{\"Manufacturer\": \"VMware, Inc.\", \"Model\": \"VMware7,1\", \"Name\": \"DC01\"}]', '[{\"Index\": \"1\", \"IPAddress\": [\"192.168.1.240,fe80::350e:d28d:14a5:5cbb\"]}]'
What is this? Where did you get it from? Apparently is this the value that variable "output_array" should hold. First make this readable.
output_array = [[{"Manufacturer": "VMware, Inc.", "Model": "VMware7,1", "Name": "DC01"}],
                [{"Index": "1", "IPAddress": ["192.168.1.240,fe80::350e:d28d:14a5:5cbb"]}]
               ]
What does that mean? It starts with a square bracket, so it is a list. Then there is a second square bracket so there is a list in a list. This nested list starts with a curly brace so it is a dictionary. The nested list contains one dictionary. This dictionary has one element with key "Name". There is also a second nested list also containing one dictionary but with completely different keys. Apparently you do nothing with that.

Leaving out all the superfluous lines you wrote (and adding the missing lines), you get this:
import json

output_array = [[{"Manufacturer": "VMware, Inc.", "Model": "VMware7,1", "Name": "DC01"}],
                [{"Index": "1", "IPAddress": ["192.168.1.240,fe80::350e:d28d:14a5:5cbb"]}]
               ]

with open('data2.json', 'w') as jsonFile:
    json.dump(output_array, jsonFile)

with open('data2.json', 'r') as lst:
    d = json.load(lst)

print(d[0][0]["Name"])
Output:
DC01
(Mar-10-2022, 08:34 AM)ilknurg Wrote: [ -> ]Traceback (most recent call last):  File "wmi_deneme.py", line 121, in <module>    print(d["Name"])TypeError: string indices must be integers

Guessing-Mode: The object, you're trying to access, is not a dict. Instead, it's a list. To get elements from a list, an Integer is required. Just try to get the first Element 0 from the list.