DEV Community

BSON to JSON: Efficient Data Conversion with Java

Through this blog post, you will learn how to convert a BSON document to JSON using Java.

BSON to JSON with Java

If you’re a Java developer, there are two ways to read BSON documents and convert them to JSON.

  • Using the MongoDB Java Driver to query an active database

  • Reading and parsing local .bson files at the byte level

Let's create a sample project.

mvn archetype:generate \
  -DgroupId=com.your-domain \
  -DartifactId=bson-to-json \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DinteractiveMode=false
Enter fullscreen mode Exit fullscreen mode

Now, add dependencies and configure the project by editing the pom.xml file as follows:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.your-domain</groupId>
  <artifactId>bson-to-json</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>bson-to-json</name>

  <properties>
    <maven.compiler.release>11</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.mongodb</groupId>
      <artifactId>mongodb-driver-sync</artifactId>
      <version>5.3.1</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-nop</artifactId>
      <version>2.0.9</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version> <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <mainClass>com.your-domain.App</mainClass>
          <cleanupDaemonThreads>false</cleanupDaemonThreads>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
Enter fullscreen mode Exit fullscreen mode
  • Lock the project to a specific Java version (in this case, Java 11) to ensure consistent compilation across different environments

      <properties>
        <maven.compiler.release>11</maven.compiler.release>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
  • Add the MongoDB driver as dependency

        <dependency>
          <groupId>org.mongodb</groupId>
          <artifactId>mongodb-driver-sync</artifactId>
          <version>5.3.1</version>
        </dependency>
    
  • Silence internal MongoDB driver logs using SLF4J-NOP

        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-nop</artifactId>
          <version>2.0.9</version>
        </dependency>
    
  • Automate dependency linking and silence MongoDB's background thread warnings by using the exec-maven-plugin

      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
            <mainClass>com.your-domain.App</mainClass>
            <cleanupDaemonThreads>false</cleanupDaemonThreads>
            </configuration>
          </plugin>
        </plugins>
      </build>
    

Using the MongoDB Java Driver to query an active database

Now, edit the App.java file stored in the src/main/java/com/your-domain/.

package com.your-domain;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.json.JsonWriterSettings;

import java.io.FileWriter;
import java.io.IOException;

public class App {
    public static void main(String[] args) {
        String uri = "mongodb://user:password@localhost:27017/?authSource=admin";

        try (MongoClient mongoClient = MongoClients.create(uri)) {
            MongoDatabase database = mongoClient.getDatabase("database");
            MongoCollection<Document> collection = database.getCollection("collection");

            JsonWriterSettings settings = JsonWriterSettings.builder().indent(true).build();

            try (FileWriter file = new FileWriter("collection.json")) {
                file.write("[\n");

                for (Document doc : collection.find()) {
                    file.write(doc.toJson(settings) + ",\n");
                }

                file.write("]");
                System.out.println("Exported successfully!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Declares the package that the Java class belongs to

    package com.your-domain;
    
  • Establish connection to the MongoDB instance

            try (MongoClient mongoClient = MongoClients.create(uri))
    

    Where uri is the connection string.

  • Target the specific database and collection from which the data will be exported

                MongoDatabase database = mongoClient.getDatabase("company");
                MongoCollection<Document> collection = database.getCollection("employees");
    
  • Pretty format the content of the JSON file

                JsonWriterSettings settings = JsonWriterSettings.builder().indent(true).build();
    
  • Manually construct and write a JSON array by wrapping the converted documents in brackets

                    file.write("[\n");
                    for (Document doc : collection.find()) {
                        file.write(doc.toJson(settings) + ",\n");
                    }
                    file.write("]");
    

Reading and parsing local .bson files at the byte level

Now, edit the App.java file stored in the src/main/java/com/your-domain/.

package com.your-domain;

import org.bson.Document;
import org.bson.BsonType;
import org.bson.codecs.DocumentCodec;
import org.bson.codecs.DecoderContext;
import org.bson.BsonBinaryReader;
import org.bson.io.ByteBufferBsonInput;
import org.bson.ByteBufNIO;
import org.bson.json.JsonWriterSettings;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class App {
    public static void main(String[] args) throws Exception {
        String inputPath = "collection.bson";
        String outputPath = "collection.json";
        JsonWriterSettings settings = JsonWriterSettings.builder().indent(true).build();
        DocumentCodec codec = new DocumentCodec();

        try (FileInputStream fis = new FileInputStream(inputPath);
             FileChannel channel = fis.getChannel();
             FileWriter writer = new FileWriter(outputPath)) {

            ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
            channel.read(buffer);
            buffer.flip();

            try (ByteBufferBsonInput bsonInput = new ByteBufferBsonInput(new ByteBufNIO(buffer));
                 BsonBinaryReader reader = new BsonBinaryReader(bsonInput)) {

                writer.write("[\n");
                boolean first = true;

                while (buffer.hasRemaining()) {

                    if (reader.readBsonType() == BsonType.END_OF_DOCUMENT) {
                        break;
                    }

                    if (!first) {
                        writer.write(",\n");
                    }

                    Document doc = codec.decode(reader, DecoderContext.builder().build());
                    writer.write(doc.toJson(settings));
                    first = false;
                }

                writer.write("\n]");
                System.out.println("Conversion completed successfully: " + outputPath);
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Initialize the file paths, configures the JSON output with indentation for readability and sets up the engine for translating binary data into Java objects

        String inputPath = "collection.bson";
        String outputPath = "collection.json";
        JsonWriterSettings settings = JsonWriterSettings.builder().indent(true).build();
        DocumentCodec codec = new DocumentCodec();
    
  • Allocates a memory buffer the exact size of the file and uses a FileChannel to load the binary data for processing

                ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
                channel.read(buffer);
                buffer.flip();
    
  • Wraps the memory buffer into a ByteBufferBsonInput and creates a BsonBinaryReader to navigate the binary structure

                try (ByteBufferBsonInput bsonInput = new ByteBufferBsonInput(new ByteBufNIO(buffer));
                  BsonBinaryReader reader = new BsonBinaryReader(bsonInput)) {
Enter fullscreen mode Exit fullscreen mode
  • Writes the opening bracket and uses a loop to iterate through the buffer until no documents remain
                  writer.write("[\n");
                  boolean first = true;

                  while (buffer.hasRemaining()) {
                      if (reader.readBsonType() == BsonType.END_OF_DOCUMENT) {
                          break;
                      }
                      // ... loop logic
                  }
Enter fullscreen mode Exit fullscreen mode
  • Translates each binary BSON segment into a Java Document and writes it to the file as a formatted JSON string
                  Document doc = codec.decode(reader, DecoderContext.builder().build());
                  writer.write(doc.toJson(settings));
Enter fullscreen mode Exit fullscreen mode

The BSON file must be in the root directory of your Maven project. Now, you can run the above code by executing the following command:

mvn clean compile exec:java
Enter fullscreen mode Exit fullscreen mode

It will do a clean build, delete the target folder, compile the Java code, and run the project.

After running, you will find the corresponding JSON file in the root directory of your project

Conclusion

If you’re a developer, you can use the MongoDB Java driver to query and analyze database collections before exporting them to JSON, or directly parse local BSON files for a quick format conversion.

Top comments (0)