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.
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 Type | Example Value | Go Type | BSON Tag Behavior |
|---|---|---|---|
| string | "hello" | string | Encoded as BSON string (UTF-8) |
| number (integer) | 42 | int64 | Encoded as BSON int64 by default |
| number (float) | 3.14 | float64 | Encoded as BSON double |
| boolean | true | bool | Encoded as BSON bool |
| null | null | interface{} | Encodes as BSON null; use *Type for nullable fields |
| object | {"k": "v"} | struct (nested) | Inline struct with its own bson tags |
| array of objects | [{...}] | []StructType | BSON array of embedded documents |
| array of strings | ["a","b"] | []string | BSON array of strings |
| array of numbers | [1,2,3] | []int64 | BSON array of int64 |
| empty object | {} | interface{} | Mapped to bson.M for dynamic documents |
BSON vs JSON vs Other Go Struct Tag Formats
| Tag Format | Package | MongoDB Support | Field Rename | Omit Empty | Inline Embedding |
|---|---|---|---|---|---|
| bson:"name" | go.mongodb.org/mongo-driver/bson | Native (required) | Yes | omitempty | inline |
| json:"name" | encoding/json | None (indirect) | Yes | omitempty | Not supported |
| bson+json (dual) | Both packages | Full + REST | Yes (both) | Yes (both) | bson only |
| mapstructure:"name" | github.com/mitchellh/mapstructure | None | Yes | No | No |
| mongo:"name" | N/A (not standard) | None | N/A | N/A | N/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.