Reputation: 1
I have converted a CSV file into JSON and I am currently trying to create a MongoDB from my JSON file.
url = "mongodb://localhost:27017"
client = MongoClient(url)
db = client.york
collection = db.inventory
with open ('Inventory.json') as f:
datastore = json.loads(f.read())
for data in datastore['inventory']:
collection.insert_one(data)
Error:
TypeError Traceback (most recent call last) in 35 datastore = json.loads(f.read()) 36 ---> 37 for data in datastore['inventory']: 38 collection.insert_one(data)
TypeError: list indices must be integers or slices, not str
Upvotes: 0
Views: 683
Reputation: 1
client = MongoClient( "mongodb://localhost:27017")
db = client['york']
collection_client = db['inventory']
with open ('Inventory.json') as f: datastore = json.loads(f.read())
collection_client.insert_many(datastore)
Upvotes: 0
Reputation: 6496
You can't do
for data in datastore['inventory']:
As the error says: list indices must be integers or slices, not str
. You probably want your datastore to be a dictionary that contains the 'inventory'
key.
Upvotes: 0