This post was originally published in April, 2023 and was updated in April, 2025.
Binary Javascript Object Notation (BSON) is a binary-encoded serialization 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 efficiently using BSON (Binary JSON), both internally and in mongodump files. However, BSON isn’t human-readable. To inspect this data, you often need to convert BSON documents to JSON. While JSON supports fewer data types, its readability is essential for debugging and analysis.
This blog post demonstrates several practical methods to convert BSON to JSON, including using MongoDB’s own tools like bsondump and mongoexport, as well as scripting solutions with Python and Bash.
Convert BSON Files 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:
1 |
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.
Export MongoDB Data as 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:
1 |
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 section in the documentation for more information.
The --pretty option will pretty format the content of the JSON file.
Convert BSON to JSON using Python (PyMongo)
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
1 2 3 4 5 6 7 8 |
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:
-
- Import the decode_all and dumpsmethods from the bsonmodule.
- Open the file to read the content and decode the data.
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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:
-
- Import the method MongoClient from the pymongo library, and the dumps method from the bson module.
- Establish the connection to the database.
- Set the database (e.g., company ) and the collection (e.g., employees) you want to query.
- 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.
- Create a JSON object by calling the dumps method. The indent = 2 parameter will tell dumps() to pretty format the JSON object.
- Write the content of the json_data variable to the data.json file.
Before running the script, you must install PyMongo: pip install pymongo.
Batch Convert BSON Files 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.
1 2 3 4 5 6 7 8 |
#!/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
- Add execution permission to the script: chmod +x bson_to_json.sh.
- Execute this command in the command line:
1 |
./bson_to_json.sh |
Conclusion
When you need to view the contents of MongoDB’s binary data format, several effective methods exist to convert BSON documents to human-readable JSON. For converting .bson files (like those from mongodump), bsondump is the dedicated tool. To export data directly from a live database as JSON, mongoexport is the standard utility. For developers needing programmatic conversion or data manipulation, Python with the PyMongo library offers flexibility, and simple Bash scripts can automate batch file conversions using bsondump. Choosing the right method depends on whether you’re working with files or a live database and your scripting preferences.
Ready to leave MongoDB, Inc. behind? Percona is the cost-effective, enterprise-grade alternative businesses trust.
FAQs: Converting BSON to JSON
Q1: What is the main difference between BSON and JSON?
A: JSON (JavaScript Object Notation) is a human-readable text format. BSON (Binary JSON) is a binary-encoded serialization format used by MongoDB for efficient storage and network transfer; it supports more data types than JSON but is not human-readable.
Q2: How can I view the contents of a .bson
file from mongodump
?
A: The easiest way is to use the bsondump
command-line tool, which is part of the MongoDB Database Tools package. It directly converts the .bson
file into a readable JSON format (e.g., bsondump file.bson --outFile=file.json
).
Q3: Can mongoexport
convert a .bson
file to JSON?
A: No, mongoexport
does not convert existing .bson
files. It connects to a running MongoDB database instance and exports data directly from a collection into a JSON or CSV file.