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 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, 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
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
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 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
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:
./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.
There are other solutions like online tools, but here you learned some ways to do it.
Top comments (0)