DEV Community

BSON to JSON: The Python Way

Through this blog post, you will learn how to convert a BSON document to JSON using Python.

BSON to JSON with Python

If you’re a Python developer, there are two ways for reading a BSON document and converting it to JSON.

  • Using the bson module from PyMongo
from bson import decode_all
from bson.json_util import dumps

with open('./data.bson','rb') as f:
    data = decode_all(f.read())

with open("./data.json", "w") as outfile:
    outfile.write(dumps(data, indent=2))
Enter fullscreen mode Exit fullscreen mode

This is what the script is doing:

  1. Import the `decode_all` and `dumps` methods from the `bson` module
  2. Open the file to read the content and decode the data
  3. Create a JSON file, and write the JSON document created from the data of the BSON file

The script works with BSON files generated by mongodump. Before running the script, you must install PyMongo: pip install pymongo.

  • Connecting to the database and querying the data with PyMongo, the Python driver for MongoDB.
from pymongo import MongoClient
from bson.json_util import dumps

uri = "mongodb://username:password@host:port/"
client = MongoClient(uri)

db = client.company
employees = db.employees

cursor = employees.find()
list_cur = list(cursor)

json_data = dumps(list_cur, indent = 2)

with open('data.json', 'w') as file:
    file.write(json_data)
Enter fullscreen mode Exit fullscreen mode

This is what the script is doing:

  1. Import the `MongoClient` method from the `pymongo` library, and the `dumps` method from the `bson` module
  2. Establish the connection to the database
  3. Set the database (e.g., `company` ) and the collection (e.g., `employees`) you want to query
  4. Retrieve the documents in the collection with the `find()` method and create a list with the result. If you don’t pass any parameter to this method, the result will be similar to `SELECT *` in MySQL
  5. Create a JSON object by calling the `dumps` method. The `indent = 2` parameter will tell `dumps()` to pretty format the JSON object
  6. Write the content of the `json_data` variable to the `data.json` file

Before running the script, you must install PyMongo: pip install pymongo.

Conclusion

If you’re a developer, you can use the MongoDB driver of your programming language of choice and query the data to analyze the content of the collections in your database. For Python, you can install PyMongo, connect to the database, query the data and use the bson module to save the content as a JSON document.

Top comments (0)