jsondecode.com logo

JSON to Go Struct with BSON Tags — Free Online Tool

Convert JSON to Go struct definitions with BSON tags for MongoDB using AI. Generates idiomatic Go code with snake_case BSON field names. Free, no sign-up.

JSON to BigQuery Schema Converter — Free Online ToolJSON to React Flow Diagram — Convert JSON Online FreeJSON to TypeScript Interface Generator — Free OnlineJSON to YAML Converter — Convert JSON to YAML OnlineJSON to CSV Converter — Export JSON Array to CSV FreeJSON to Python Dataclass Generator — Free Online ToolJSON to SQL INSERT Statement Generator — Free OnlineJSON to Markdown Table Converter — Free Online ToolJSON to XML Converter — Convert JSON to XML Online FreeJSON to HTML Table Converter — Free Online ToolYAML to JSON Converter — Convert YAML to JSON Online FreeXML to JSON Converter — Convert XML to JSON Online FreeJSON to JSON Schema Generator — Free Online ToolJSON to GraphQL Schema Generator — Free Online ToolRuby to JSON Converter — Convert Ruby Hashes to JSON OnlineJSON to C# Class Generator — Free Online ToolJSON to Java Class (POJO) Generator — Free Online ToolJSON to Kotlin Data Class Generator — Free Online ToolJSON to Rust Struct Generator — Free Online ToolJSON to PHP Array Converter — Free Online ToolCSV to JSON Converter — Free Online ToolJSON to Dart Class Generator — Free Online ToolJSON to Swift Struct (Codable) Generator — Free OnlineJSON to Terraform HCL Variables Converter — Free OnlineJSON to Mongoose Schema Generator — Free Online ToolJSON to Prisma Schema Model Generator — Free OnlineJSON to Protocol Buffer (proto3) Generator — Free OnlineJSON to TOML Config Format Converter — Free Online ToolTOML to JSON Converter — Convert TOML to JSON Online FreeJSON to Apache Avro Schema Generator — Free Online ToolJSON to OpenAPI 3.0 Schema Component — Free Online ToolJSON to R Data Frame Code Generator — Free Online ToolJSON to Lua Table Syntax Converter — Free Online ToolJSON to Zod Schema (TypeScript) Generator — Free OnlineJSON to Scala Case Class with Circe Codec — Free OnlineJSON to PowerShell Hashtable Converter — Free Online Tool

Related Guides

JSON to Go Struct with BSON Tags — Free Online Tool

About JSON to Go Struct with BSON Tags — Free Online Tool

This tool converts JSON data into Go struct definitions annotated with BSON tags, making it immediately usable with the official MongoDB Go driver (go.mongodb.org/mongo-driver). Developers use it to eliminate the tedious, error-prone work of writing struct field tags by hand when building Go applications that read or write documents to MongoDB.

JSON to Go BSON Type Mapping

JSON TypeExample ValueGo TypeBSON Tag Behavior
string"hello"stringEncoded as BSON string (UTF-8)
number (integer)42int64Encoded as BSON int64 by default
number (float)3.14float64Encoded as BSON double
booleantrueboolEncoded as BSON bool
nullnullinterface{}Encodes as BSON null; use *Type for nullable fields
object{"k": "v"}struct (nested)Inline struct with its own bson tags
array of objects[{...}][]StructTypeBSON array of embedded documents
array of strings["a","b"][]stringBSON array of strings
array of numbers[1,2,3][]int64BSON array of int64
empty object{}interface{}Mapped to bson.M for dynamic documents

BSON vs JSON vs Other Go Struct Tag Formats

Tag FormatPackageMongoDB SupportField RenameOmit EmptyInline Embedding
bson:"name"go.mongodb.org/mongo-driver/bsonNative (required)Yesomitemptyinline
json:"name"encoding/jsonNone (indirect)YesomitemptyNot supported
bson+json (dual)Both packagesFull + RESTYes (both)Yes (both)bson only
mapstructure:"name"github.com/mitchellh/mapstructureNoneYesNoNo
mongo:"name"N/A (not standard)NoneN/AN/AN/A

Frequently Asked Questions

What is a BSON tag in Go and why do I need it for MongoDB?

A BSON tag is a struct field annotation like `bson:"field_name"` that tells the MongoDB Go driver how to serialize and deserialize each field when reading from or writing to a MongoDB collection. Without BSON tags, the driver defaults to lowercasing field names, which can cause mismatches with existing documents that use camelCase or snake_case keys. Adding explicit BSON tags gives you full control over field naming and behavior like omitempty.

How do I use a generated Go BSON struct with the MongoDB Go driver?

After generating the struct, import go.mongodb.org/mongo-driver/bson and pass your struct directly to collection.InsertOne or collection.FindOne — the driver uses reflection and the bson tags to marshal/unmarshal automatically. For example: `var result MyStruct; collection.FindOne(ctx, filter).Decode(&result)`. No additional configuration is needed beyond having the bson tags present.

What does omitempty do in a BSON tag?

The omitempty option in a BSON tag (`bson:"fieldName,omitempty"`) causes the field to be excluded from the BSON document entirely when it holds a zero value — empty string, 0, false, nil, or empty slice. This is useful to avoid storing null or default fields in MongoDB, keeping documents smaller and queries cleaner. Be careful with pointer types: a nil pointer is omitted, but a pointer to a zero value is not.

How do I map a MongoDB ObjectID (_id field) in a Go struct?

The _id field in MongoDB should be mapped as `ID primitive.ObjectID \`bson:"_id,omitempty"\`` using the primitive package from go.mongodb.org/mongo-driver/bson/primitive. The omitempty option lets MongoDB generate the ObjectID automatically on insert if you leave the field at its zero value. Import the primitive package separately: `import "go.mongodb.org/mongo-driver/bson/primitive"`.

Can I use both json and bson tags on the same Go struct field?

Yes, Go supports multiple struct tags on a single field, and this is a common pattern for structs that are both stored in MongoDB and serialized to JSON for a REST API. Write them side by side: `Name string \`json:"name" bson:"name"\``. Each package reads only its own tag and ignores the others, so json.Marshal and the MongoDB driver will each use their respective tag values independently.

If jsondecode.com saved you time, share it with your team

Free forever. No ads. No sign-up. Help other developers find it.