JSON Tool 08

JSON to Go structs

Generate Go structs from a JSON sample, with json tags, pointer fields for nullable keys and mixed-type fields widened instead of dropped. The output is formatted the way gofmt would leave it.

Input Enter JSON structure

Paste the JSON your API returns

Keys become exported fields with the original key in the json tag
Go Structs will appear here

Structs appear here

Press Generate structs to build a single .go file
Waiting for input Client-side only

Frequently asked

What happens when the same field has different types in different array elements?
The field widens to the type that can hold both — int and float64 become float64 — and when no single type fits (a string in one element, a number in another) it falls back to interface{} / any so the code still compiles. The status bar lists which fields were widened.
When should I turn on pointer fields?
Turn it on when a field can be null in your API and you need to tell "absent" from "zero value" — for example an optional deleted_at. A *string can be nil; a plain string cannot, so the distinction is lost. The toggle applies only to fields that actually had a null in your sample.
Does it produce interface{} or any?
Your choice — the Empty type dropdown switches between them. any is an alias added in Go 1.18, so pick it only if your module targets 1.18 or newer. Nothing else in the generated code differs.
How are JSON keys that are not valid Go identifiers handled?
The struct field name is converted to an exported PascalCase identifier and the original key is kept in the json tag, so user-id and other non-ASCII keys still produce valid code. Keys starting with a digit get an N prefix, and non-ASCII keys get a positional name, because Go identifiers must be letters, digits or underscores and must not start with a digit.
Is my JSON uploaded?
No. The structs are generated by JavaScript in the page and the .go download is built from a local Blob. The tool makes no network request with your document.