MongoDB: How to Convert BSON Documents to a Human-readable Format
Tags: mongodb, python, bash
Published: 2023-04-03 19:42:35

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](https://www.mongodb.com/docs/database-tools/bsondump/) converts [BSON](https://www.mongodb.com/docs/manual/reference/glossary/#std-term-BSON) files into human-readable formats, including [JSON](https://www.mongodb.com/docs/manual/reference/glossary/#std-term-JSON). For example, bsondump is useful for reading the output files generated by [mongodump](https://www.mongodb.com/docs/database-tools/mongodump/#mongodb-binary-bin.mongodump). The bsondump tool is part of the [MongoDB Database Tools](https://www.mongodb.com/docs/database-tools/installation/installation/) package.

Run `bsondump` from the system command line:

```
bsondump --outFile=collection.json collection.bson
```

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](https://www.mongodb.com/docs/database-tools/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](https://www.mongodb.com/docs/database-tools/installation/installation/) package.

Run `mongoexport` from the command line:

```
mongoexport --collection=employees --db=company --out=employees.json --pretty
```

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](https://www.mongodb.com/docs/database-tools/mongoexport/#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](https://pymongo.readthedocs.io/en/stable/)

```python
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))
```

This is what the script is doing:
<ol type="a">
  <li>Import the `decode_all`  and `dumps` methods from the `bson` module</li>
  <li>Open the file to read the content and decode the data</li>
  <li>Create a JSON file, and write the JSON document created from the data of the BSON file</li>
</ol>

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

2. Connecting to the database and querying the data with PyMongo, the Python driver for MongoDB.

```python
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)
```

This is what the script is doing:

<ol type="a">
  <li>Import the `MongoClient` method from the `pymongo` library, and the `dumps` method from the `bson` module</li>
  <li>Establish the connection to the database</li>
  <li>Set the database (e.g., `company` ) and the collection (e.g., `employees`) you want to query</li>
  <li>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</li>
  <li>Create a JSON object by calling the `dumps` method. The `indent = 2` parameter will tell `dumps()` to pretty format the JSON object</li>
  <li>Write the content of the `json_data`  variable to the `data.json` file</li>
</ol>

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

## BSON to JSON with Bash
I asked the AI at [phind.com](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
```

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
```

## 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](https://www.mongodb.com/docs/database-tools/installation/installation/).

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!](https://www.percona.com/software/mongodb)