ToolZoneX
Blog

JSON to Go BSON Struct Converter

Paste a JSON sample to instantly generate matching Go structs tagged with both json and bson, for use with the MongoDB Go driver. Free online developer tool.

Paste JSON
Go Struct (BSON + JSON)
Go struct definitions will appear here...

Free JSON to Go BSON Struct Converter

Paste a JSON sample to generate Go structs tagged for both encoding/json and the MongoDB Go driver — each field gets a json:"..." tag alongside abson:"..." tag, so the same struct can decode an API payload and read or write a MongoDB document.

How to Use It

Paste any JSON object or array, or click "Load Example." The tool builds one struct per object level with PascalCase field names, nested objects as their own named structs, slices for arrays, and pointer types for fields that are optional or nullable in your sample — exactly like the plain Go Struct converter, but with a bson tag added next to every json tag.

Example

Given {"name": "Ann", "age": 30, "tags": ["a","b"], "address": {"city": "NYC", "zip": null}}, the Name field is tagged`json:"name" bson:"name"`, so the struct works whether it's decoded from a JSON request body or a document returned by mongo.Collection.FindOne.

Common Use Cases

  • Modeling a MongoDB collection's documents as Go structs for the official Go driver.
  • Sharing one struct between an HTTP API layer and a MongoDB persistence layer without duplicating tags by hand.
  • Scaffolding types quickly from a sample document exported from a MongoDB collection.

FAQs

Why include both json and bson tags?

In practice, most Go services that talk to MongoDB also expose or consume JSON over HTTP using the same struct. Adding both tags up front means you don't have to double back and add the missing one later.

Does this work with the official MongoDB Go driver?

Yes — the bson:"..." tag format matches what go.mongodb.org/mongo-driverexpects for marshaling and unmarshaling BSON documents.

Is my JSON uploaded anywhere?

No — parsing and struct generation happen entirely client-side in your browser. Nothing you paste is sent to a server.