0

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.

2
  • try to print row[0] and row[1] and see what they are , you can use enumerate and use index no there. Nov 2, 2017 at 5:52
  • @AyodhyankitPaul row[0] gives me the ID and row[1] the json string which is of type str type(row[1]) Out[119]: str
    – Domnick
    Nov 2, 2017 at 6:57

1 Answer 1

2

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.