/

JSON to Go

Processed Client Side

Generate idiomatic Go structs with json tags from a JSON sample, with nested types inferred.

JSON · 11 lines
11 lines · 154 chars
Length: 154Lines: 11Size: 154 BytesCursor: 1:1
Go
Go
goLength: 304Lines: 14Size: 304 BytesCursor: 1:1

Bookmark this tool now — skip the search next time you need it.

About JSON to Go

This tool runs entirely in your browser. Whatever you paste is processed on your own device and is never uploaded, logged, or sent to any server.

Generate idiomatic Go structs from a JSON sample, complete with json struct tags so the result unmarshals correctly without further editing. Field names are converted to exported Go identifiers while the tag preserves the original JSON key, which is exactly the boilerplate that makes hand-writing structs for an unfamiliar API tedious. Nested objects become their own named struct types, arrays become slices of the right element type, and whole numbers map to int while anything with a decimal component maps to float64. Where a value is genuinely ambiguous — an empty array, or a field holding more than one type — it falls back to interface{} rather than guessing wrong.

Key features

  • Generates Go structs with json struct tags that preserve the original key names
  • Nested objects become their own named struct types
  • Arrays become slices with the element type inferred from the contents
  • Whole numbers map to int, values with a decimal component map to float64
  • Genuinely ambiguous values fall back to interface{} rather than an incorrect concrete type
  • Configurable root struct name
  • Syntax-highlighted Go output ready to paste into a package
  • Parse errors report the offending line in the JSON input
  • Runs entirely in your browser — nothing you paste is uploaded

How to use it

  1. Paste a representative JSON sample into the input pane.
  2. Set the root struct name to something meaningful.
  3. Click Convert.
  4. Copy the generated Go and paste it into your package.

Tips & common mistakes

  • Check every int carefully. A field that happens to hold a whole number in your sample may hold a decimal in production, and unmarshalling will then fail at runtime.
  • An interface{} in the output marks a spot the sample could not resolve — usually an empty array or a mixed-type field. Those are the fields to pin down by hand.
  • Add omitempty to tags for fields that should be left out when empty. The generator emits plain tags, which is the right default but rarely the whole story for request payloads.
  • Go zero values hide missing fields: an absent JSON number unmarshals to 0, indistinguishable from a real 0. Use a pointer type where you need to tell those apart.
  • Only exported fields — those starting with a capital letter — are unmarshalled by encoding/json. The generator handles this, but it is worth knowing if you rename anything afterwards.
  • Dates arrive as strings. Either keep them as string and parse explicitly, or use a custom type with its own UnmarshalJSON.
  • Starting from YAML instead of JSON? Generate the same Go structs directly with YAML to Go.

Related tools

Browse all 24 JSON tools

Frequently asked questions

10

Paste a JSON payload, set the struct name, and press Convert. The tool outputs idiomatic Go structs with json struct tags, ready to paste into your package.

Yes. Field names are exported in PascalCase and common initialisms like ID, URL, and API are upper-cased, matching the Go style guide.

Whole numbers become int and numbers with decimals become float64. If a field can be either, use a decimal value in your sample to force float64.

Each nested object becomes its own named struct, referenced by a pointer, so omitted objects and zero values remain distinguishable.

A field holding a whole number in your sample may hold a decimal in production. Unmarshalling then fails at runtime, so widen those to float64 where the value could ever have a fractional part.

It marks a value the sample could not resolve — usually an empty array or a field holding more than one type. Those are exactly the fields worth pinning down by hand.

For request payloads, usually yes, so empty fields are left out. The generator emits plain tags, which is the correct default for decoding responses but rarely the whole story for encoding.

Use a pointer type. An absent JSON number unmarshals into 0, indistinguishable from a real 0, and only a nil pointer can express the difference.

Only exported fields are unmarshalled by encoding/json. The json tag preserves the original lowercase key, so the wire format is unchanged.

They arrive as strings. Either keep them as string and parse explicitly, or define a custom type with its own UnmarshalJSON method.