Nat
Nat

Reputation: 749

Import json file to mongoDB using python

I am getting this error while trying to import:

Traceback (most recent call last):
  File "C:\PythonSC\module21.py", line 8, in <module>
    page = open('c:\restaurants\restaurants.json','r')
OSError: [Errno 22] Invalid argument: 'c:\restaurants\restaurants.json'

here is my source code of trying to import:

import json
import pymongo
from pymongo import MongoClient

client = MongoClient('localhost', 27017)
db = client.restaurants
collection = db.restaurantsCollection
page = open('c:\restaurants\restaurants.json','r')
dataset = json.loads(page.read())

for item in dataset:
    collection.insert(item)

for item in collection.find():
    print(item)

and here is the sample json data from https://raw.githubusercontent.com/mongodb/docs-assets/geospatial/restaurants.json

I still could not understand why this error and I hope someone could lend me a helping hand. Thanks a lot.

Upvotes: 0

Views: 118

Answers (1)

Akhil kumar Amarneni
Akhil kumar Amarneni

Reputation: 230

1.Place your json file in your project repository and then use it for reading.

f= open("[file_name]","r+")

2.Other way(Second Method) is to use os module to get position of file and use it as path=os.getcwd()+[append your file name] open(path,'r+');

Upvotes: 1

Related Questions