Skip to content

JSON to Go struct converter

Paste a JSON sample. Go structs with json tags are written beside it.

Structs come from one sample. Add a package line, and check that numbers typed int64 never arrive with a fraction.

Ctrl+Enter or ⌘+Enter runs this tool immediately.

JSON sample
Go

Waiting for input.

Parsing stays in this browser. Input is not uploaded or saved.

How to use this tool

  1. Paste an API response or config file, or load the sample.
  2. Read the structs. Each nested object becomes its own named struct, and field names follow Go style, such as user_id to UserID.
  3. Copy the code into a .go file under your package line, then adjust types where the sample was not representative.

Supported formats and limitations

  • Whole numbers become int64 and other numbers become float64. A field that is 1 in the sample but 1.5 elsewhere needs float64.
  • A field that is only null in the sample becomes any. A field that is sometimes null becomes a pointer, such as *string.
  • Objects in the same array are merged into one struct. A key missing from some of them gets omitempty in its tag.
  • Mixed arrays, such as strings and numbers together, become []any.
  • Field names that would repeat after conversion get a number suffix. The json tag always keeps the original key.

Examples

An optional field

gift_note appears in one item, so it becomes GiftNote string `json:"gift_note,omitempty"`.

{"items": [{"sku": "A"}, {"sku": "B", "gift_note": "hi"}]}

A root array

The array becomes type Root []RootItem, and each item becomes a RootItem struct.

[{"id": 1}, {"id": 2}]

Common errors and solutions

Expected double-quoted property name
The sample is not strict JSON. Remove trailing commas and comments, or copy the raw response body.

FAQ

Why is a field typed any?
The sample only had null for it, so its real type is unknown. Replace any with the type you expect, usually a pointer such as *string.
Does the output pass gofmt?
Fields are indented with tabs and aligned in columns as gofmt does. Add a package line before compiling.
Is the JSON uploaded?
No. Structs are generated in your browser.

Related tools

  • JSON to TypeScript converter

    Generate TypeScript interfaces from a JSON sample in the browser. Array items are merged, and keys missing from some items become optional.

  • JSON Schema generator

    Generate a draft 2020-12 JSON Schema from a JSON sample in the browser. Types, nested objects, array items, and required keys are inferred.

  • JSON formatter

    Validate JSON, then pretty-print it with 2 or 4 spaces or minify it. Invalid JSON produces an error and leaves the output empty.