Python Forum
string indices must be integers when parsing Json
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string indices must be integers when parsing Json
#1
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
Reply
#2
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.
Reply
#3
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
Reply
#4
(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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Parsing large JSON josvink66 5 627 Jan-10-2024, 05:46 PM
Last Post: snippsat
  tuple indices must be integers or slices, not str cybertooth 16 11,455 Nov-02-2023, 01:20 PM
Last Post: brewer32
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,157 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,231 Jun-09-2023, 06:56 PM
Last Post: kpatil
  [split] Parse Nested JSON String in Python mmm07 4 1,521 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,368 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 2,011 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,455 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: string indices must be integers JonWayn 12 3,383 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  Parsing JSON pyStund 4 2,965 Jul-31-2022, 02:02 PM
Last Post: pyStund

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020