使用python在mongodb中上传本地json文件

时间:2018-11-06 23:13:27

标签: python json mongodb

我已经在AWS上安装了mongoDB,并且有兴趣从mongoDB上的本地计算机上传文件。我已经使用终端在AWS上打开了mongoDB,并创建了SSH隧道来建立与AWS上运行的mongoDB的连接。

我正在使用以下代码在mongoDB上上传本地json文件

data_path="/Users/username/Desktop/file.json"

with open(data_path) as f:
    file_data = json.load(f)

db.collectionname.insert(file_data) client.close()

我遇到以下错误

<ipython-input-23-0317fba0bf38> in <module>()
      2 
      3 with open(data_path) as f:
----> 4     file_data = json.load(f)
      5 
      6 reviews.insert(file_data)

~/anaconda3/lib/python3.6/json/__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    297         cls=cls, object_hook=object_hook,
    298         parse_float=parse_float, parse_int=parse_int,
--> 299         parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
    300 
    301 

~/anaconda3/lib/python3.6/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    352             parse_int is None and parse_float is None and
    353             parse_constant is None and object_pairs_hook is None and not kw):
--> 354         return _default_decoder.decode(s)
    355     if cls is None:
    356         cls = JSONDecoder

~/anaconda3/lib/python3.6/json/decoder.py in decode(self, s, _w)
    340         end = _w(s, end).end()
    341         if end != len(s):
--> 342             raise JSONDecodeError("Extra data", s, end)
    343         return obj
    344 

JSONDecodeError: Extra data: line 2 column 1 (char 394)

有人可以引导我吗? 谢谢

0 个答案:

没有答案