DEV Community

Mario García
Mario García

Posted on • Originally published at percona.com

MongoDB: How to Convert BSON Documents to a Human-readable Format

Binary Javascript Object Notation (BSON) is a bin­ary-en­coded seri­al­iz­a­tion of JSON documents. JSON is easier to understand as it is human-readable, but compared to BSON, it supports fewer data types. BSON has been extended to add some optional non-JSON-native data types, like dates and binary data.

MongoDB stores data in BSON format both internally and over the network. It is also the format used for the output files generated by mongodump. To read the content of a BSON document, you have to convert it to a human-readable format like JSON.

Through this blog post, you will learn how to convert a BSON document to JSON. Some of the methods I will explain include using bsondump, mongoexport, Python, and Bash.

BSON to JSON with bsondump

The bsondump converts BSON files into human-readable formats, including JSON. For example, bsondump is useful for reading the output files generated by mongodump. The bsondump tool is part of the MongoDB Database Tools package.

Run bsondump from the system command line:

bsondump --outFile=collection.json collection.bson
Enter fullscreen mode Exit fullscreen mode

It will create a JSON file (collection.json) from an existing BSON document (collection.bson), like the ones created after backing up your database.

BSON to JSON with mongoexport

mongoexport is a command-line tool that produces a JSON or CSV export of data stored in a MongoDB instance. The mongoexport tool is part of the MongoDB Database Tools package.

Run mongoexport from the command line:

mongoexport --collection=employees --db=company --out=employees.json --pretty
Enter fullscreen mode Exit fullscreen mode

To connect to a local MongoDB instance running on port 27017, you do not have to specify the host or port. If otherwise needed, check the Connect to a MongoDB Instance section in the documentation for more information.

The --pretty option will pretty format the content of the JSON file.

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.

  1. 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.

  1. 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.

BSON to JSON with Bash

I asked the AI at phind.com to tell me how to convert a BSON file to JSON, and one of the solutions that it showed me was to create a Bash script in the directory where the BSON files are.

#!/bin/bash
declare -a bson_files
bson_files=( $(ls -d $PWD/*.bson) )

for file in "${bson_files[@]}"; 
do 
bsondump $file --outFile=$file.json
done
Enter fullscreen mode Exit fullscreen mode

The script lists all the BSON files in the present directory and saves the result in an array, then loops through the array and converts every BSON file to JSON files. The script uses bsondump.

To run the script

  1. Add execution permission to the script: chmod +x bson_to_json.sh
  2. Execute this command in the command line:
./bson_to_json.sh
Enter fullscreen mode Exit fullscreen mode

Conclusion

If you want to read the content of a BSON document, you can use bsondump and mongoexport to convert a BSON document to a human-readable format like JSON. These tools are part of the MongoDB Database Tools.

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.

There are other solutions like online tools and methods provided by other programming languages, but here you learned some ways to do it.


Percona Distribution for MongoDB is a freely available MongoDB database alternative, giving you a single solution that combines the best and most important enterprise components from the open source community, designed and tested to work together.

Download Percona Distribution for MongoDB Today!

Top comments (2)

Collapse
 
kate_vach profile image
katerine valencia

Gracias!!!!! me has ayudado mucho

Collapse
 
_514b2066c68495f6b65eb profile image
凌嘉徽

Helps a lot!