CalculatorsConvertersDeveloperTextAll toolsDirectory
Submit your tool Sign in
Theme
Home/Answers/Developer
How-to

How do I generate Go structs from a JSON sample?

Short answer

Paste a sample of the JSON and generate matching Go struct definitions: exported field names, inferred types, nested structs for nested objects, slices for arrays, and json tags mapping each field back to its original key. A browser-based JSON to Go converter does this instantly on your own device, so API payloads are never pasted to a server.

Why the struct needs json tags

Go field names must be exported (capitalized) to be visible to the encoding/json package, but JSON keys are usually lowercase or snake_case. The json tag bridges the two: a field declared as UserName string with the tag json:"user_name" unmarshals correctly. A generator writes all of this for you from a sample payload, inferring numbers, strings, booleans, nested structs and slices, which removes the most tedious and typo-prone part of wiring up an API client.

Mind the limits of inference

Generation works from the one sample you provide. A field that happens to be null in the sample, or an optional field missing from it, cannot be typed with certainty, so skim the output and adjust types where you know the API contract better than the sample shows. The same workflow exists for TypeScript if the other side of your stack needs interfaces from the same payload.

Payloads stay on your machine

Real API responses contain real data. The conversion runs entirely in your browser, so the sample payload is never transmitted anywhere.

Step by step

  1. Copy a sample payload. Grab a representative JSON response from the API you are integrating.
  2. Paste it into the converter. Open the JSON to Go struct tool and paste the sample.
  3. Copy the structs. Copy the generated struct definitions with json tags into your Go code.
  4. Review the types. Check fields that were null or missing in the sample and adjust their types to match the real contract.

Frequently asked questions

Does it handle nested objects and arrays?

Yes. Nested objects become nested struct types and arrays become slices, inferred from the sample.

Why are the generated field names capitalized?

Go only exports capitalized fields to the JSON package, and the json tag keeps the mapping to the original lowercase key.

Is my payload uploaded?

No. The conversion runs entirely in your browser.

Related answers