Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, Convert bson output of mongodump to array of json objects (dictionaries)

I have dumped a mongodb collection using the mongodump command. The output is a dump directory which has these files:

dump/
    |___coll.bson
    |___coll.metadata.json

How can I open the exported files to a array of dictionaries that work in python? I tried the following and none worked:

with open('dump/coll.bson', 'rb') as f:
    coll_raw = f.read()
import json
coll = json.loads(coll_raw)

# Using pymongo
from bson.json_util import loads
coll = loads(coll_raw)

ValueError: No JSON object could be decoded
like image 631
CentAu Avatar asked Dec 16 '15 19:12

CentAu


1 Answers

You should try:

from bson import BSON
with open('dump/coll.bson', 'rb') as f:
    coll_raw = f.read()

coll = bson.decode_all(coll_raw) 
like image 91
Yash Mehrotra Avatar answered Oct 16 '22 01:10

Yash Mehrotra