简体   繁体   中英

Python Error:“TypeError: string indices must be integers”

I'm here trying to parse JSON string that comes from a sql table, but while parsing it is giving the error TypeError: string indices must be integers at line result = json_normalize(json_st,'results')

here is the code snippet

from pandas.io.json import json_normalize
import pyodbc
cnxn = pyodbc.connect('connection string')
cursor = cnxn.cursor()

cursor.execute("select TOP 1 GEOCODE_ID, JSON from GEOCODE_TBL where GEOCODE_ID = 20")
ID=[]
JSON=[]

for row in cursor.fetchall():
      ID.append(row[0])
      JSON.append(row[1])


json_st1 = json.dumps(JSON)
json_st=json.loads(json_st1)

result = json_normalize(json_st,'results')

Any suggestion on this will be helpful.

Thanks

Domnick.

Looking at the function signature, it takes one data variable and some optional params.

pandas.io.json.json_normalize(data, record_path=None, meta=None, 
meta_prefix=None, record_prefix=None, errors='raise', sep='.')

Where data : dict or list of dicts . So you can't pass it as a string . Also, why are you passing the string 'results' too?

Your data variable needs to be a dict, or list of dicts (which is, json in python)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM