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.
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.
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.
If you’re a Python developer, there are two ways for reading a BSON document and converting it to JSON.
|
1 |
from bson import decode_all<br>from bson.json_util import dumps<br><br>with open('./data.bson','rb') as f:<br> data = decode_all(f.read())<br><br>with open("./data.json", "w") as outfile:<br> outfile.write(dumps(data, indent=2)) |
This is what the script is doing:
The script works with BSON files generated by mongodump. Before running the script, you must install PyMongo: pip install pymongo.
|
1 |
from pymongo import MongoClient<br>from bson.json_util import dumps<br><br>uri = "mongodb://username:password@host:port/"<br>client = MongoClient(uri)<br><br>db = client.company<br>employees = db.employees<br><br>cursor = employees.find()<br>list_cur = list(cursor)<br><br>json_data = dumps(list_cur, indent = 2)<br><br>with open('data.json', 'w') as file:<br> file.write(json_data) |
This is what the script is doing:
Before running the script, you must install PyMongo: pip install pymongo.
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 |
#!/bin/bash<br>declare -a bson_files<br>bson_files=( $(ls -d $PWD/*.bson) )<br><br>for file in "${bson_files[@]}"; <br>do <br>bsondump $file --outFile=$file.json<br>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 |
./bson_to_json.sh |
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.
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.
Resources
RELATED POSTS