Command Palette

Search for a command to run...

JS

JSON to Go Struct

Convert JSON into Go struct definitions with json tags. Nested objects become inline structs, arrays of objects are merged, and field names follow Go conventions. Runs client-side.

What is a JSON to Go struct converter?

A JSON to Go struct converter turns a JSON document into the Go type declarations that describe it, so you can unmarshal the same data with encoding/json instead of typing the struct by hand. Every JSON object becomes a Go struct, every field carries a json:"key" tag that maps it back to the original name, and the scalar types are inferred from the values: a JSON string becomes string, a whole number becomes int, a decimal becomes float64, true and false become bool, and null becomes interface{}. Paste a payload and you get a ready-to-compile type block back.

It exists because writing Go structs from a real API response is tedious and easy to get subtly wrong. Field names have to be exported and follow Go conventions, so id becomes ID and user_url becomes UserURL, while the json tag has to preserve the exact original key or json.Unmarshal silently skips the field. Nested objects need their own struct, and an array of objects needs a single struct that covers every key any element has. Doing that across a deeply nested response is where mistakes creep in. The converter applies these rules consistently: nested objects become inline structs, and arrays of objects are merged so a key that appears in only some elements is still present in the generated type.

Everything runs in your browser. The JSON you paste is parsed and analysed in JavaScript on your own device, so nothing is uploaded, logged, or stored, and the tool keeps working with the network disconnected once the page has loaded. That matters when the payload is a private API response or contains keys and values you would rather not send anywhere. You can rename the root type, switch integers to int64 when you need the wider type, and add omitempty to every tag when you want zero values omitted from the encoded output.

A short example shows the go JSON to struct mapping. Paste {"order_id": 1042, "total": 19.5, "items": [{"sku": "A1", "qty": 2}, {"sku": "B2", "qty": 1, "gift_note": "Happy birthday"}]}, type order as the Root type and click Convert. You get type Order struct with OrderID int, Total float64 and Items []struct holding Sku string, Qty int and GiftNote string. GiftNote is there even though only the second item has it, because the JSON to Go struct converter merges every element of the array into one type.

How to use the JSON to Go struct converter?

  1. Paste your JSON

    Paste a JSON object or array into the input box. Load the sample first if you want to see the shape of the generated struct before using your own data.

  2. Name the root type

    Enter a name for the top-level struct, such as User or ApiResponse. It is cleaned into a valid exported Go identifier so it compiles as written.

  3. Set integer and tag options

    Choose int or int64 for whole numbers, and turn on omitempty if you want empty fields left out when the struct is encoded back to JSON.

  4. Convert and copy

    Click Convert to build the struct definitions, then copy them into your Go file. Invalid JSON is reported with the exact parser message so you can fix it.

Key Features

  • Correct json tags

    Every field keeps a json:"original_key" tag so json.Unmarshal maps the data back to the exact JSON name, even when the Go field is renamed to follow conventions.

  • Go-convention field names

    Keys are exported and common initialisms are uppercased, so id becomes ID, user_url becomes UserURL, and html_body becomes HTMLBody, matching golint expectations.

  • Inline nested structs

    Nested JSON objects become inline struct literals to any depth, so the whole shape lives in one declaration you can paste as a single block.

  • Merged arrays of objects

    An array of objects produces one struct covering every key any element has, and conflicting value types for a key fall back to interface{} rather than failing.

Frequently Asked Questions

A JSON to Go struct converter transforms a JSON document into the Go type declarations that describe it. Each object becomes a struct, each field gets a json:"key" tag, and the value types are inferred, so you can unmarshal the same data with encoding/json without writing the struct by hand.

Comments

0 comments

0/2000 characters

No comments yet. Be the first to share your thoughts!

json to gojson to go structgo json to structjson to golang structgo struct generatorjson to structconvert json to gogolang struct from jsonfree json to gono signup json to go