Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create JSON file from MongoDB document using Python

I am using MongoDB 3.4 and Python 2.7. I have retrieved a document from the database and I can print it and the structure indicates it is a Python dictionary. I would like to write out the content of this document as a JSON file. When I create a simple dictionary like d = {"one": 1, "two": 2} I can then write it to a file using json.dump(d, open("text.txt", 'w')) However, if I replace d in the above code with the the document I retrieve from MongoDB I get the error

ObjectId is not JSON serializable

Suggestions?

like image 730
Phil O Avatar asked Jan 04 '17 14:01

Phil O


People also ask

How do you create a JSON file in Python?

Method 2: Writing JSON to a file in Python using json.dump() Another way of writing JSON to a file is by using json. dump() method The JSON package has the “dump” function which directly writes the dictionary to a file in the form of JSON, without needing to convert it into an actual JSON object.

How do I export data from MongoDB to JSON?

mongoexport command is used to export MongoDB Collection data to a CSV or JSON file. By default, the mongoexport command connects to mongod instance running on the localhost port number 27017. Field_name(s) - Name of the Field (or multiple fields separated by comma (,) ) to be exported.

How do you convert a string to a JSON object in Python?

Use the json.loads() function. The json. loads() function accepts as input a valid string and converts it to a Python dictionary. This process is called deserialization – the act of converting a string to an object.


2 Answers

As you have found out, the issue is that the value of _id is in ObjectId.

The class definition for ObjectId is not understood by the default json encoder to be serialised. You should be getting similar error for ANY Python object that is not understood by the default JSONEncoder.

One alternative is to write your own custom encoder to serialise ObjectId. However, you should avoid inventing the wheel and use the provided PyMongo/bson utility method bson.json_util

For example:

from bson import json_util 
import json 

json.dump(json_util.dumps(d), open("text.json", "w"))
like image 60
Wan Bachtiar Avatar answered Oct 17 '22 11:10

Wan Bachtiar


The issue is that “_id” is actually an object and not natively deserialized. By replacing the _id with a string as in mydocument['_id'] ='123 fixed the issue.

like image 35
Phil O Avatar answered Oct 17 '22 10:10

Phil O