'Cant read JSON file imported from MongoDB using pandas
I am trying to read a json file into a pandas dataframe, the import from MongoDB from completed as below
client = pymongo.MongoClient("localhost", 27017)
db = client.airport_database
c = db.clean_airports_outputs
#this next line was to remove the extra line '_id'
cursor =c.find({}, {'_id': False})
docs = list(cursor)
json_data = json_util.dumps(docs, indent = 4)
with open('clean_airports_outputs.json', 'w') as file:
file.write(json_data)
messagebox.showinfo("File Imported", "\n'clean_airports_outputs.json' imported")
when I try to read this file with pandas, I get
"TypeError: list indices must be integers or slices, not str"
My original json file before I imported to MongoDB was this,

however the exported json file I am writing becomes this,

I want to write the file as per the first picture without the outer array wrapper square brackets, any thoughts?
Solution 1:[1]
my apologies all, I have worked it out
docs = list(cursor)
docs = docs[0] #this line will remove my square brackets
json_data = json_util.dumps(docs, indent = 4)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ONE-WINGED |
