Dev · 108 tools · nothing uploads

JSON to Go

JSON to Go. Infers types from an example JSON document. Nested objects become named types by their key; mixed-type arrays fall back to a broad/any type. Runs on your device.

Input

live

JSON to Go turns one example document into struct declarations, tagging every field with json and the key exactly as your data spells it. Whole numbers become int64, fractional ones float64, and a null becomes interface{}. No package clause, no imports, no pointers and no omitempty, so an absent key decodes to the zero value.

Questions

What does it generate?

Go struct types, one per object in your example, each field carrying a json tag with the original key. The top-level struct is called Root and nested structs are named after the key holding them. Structs are printed with nested ones first, so the file compiles top to bottom.

Which Go types does it use?

A string becomes string, a boolean becomes bool, a whole number becomes int64 and a fractional one becomes float64. Arrays become slices of the element type taken from the first entry. A null becomes interface{}, and so does an empty array's element type.

How are field names derived?

The key has every non-alphanumeric character removed and its first letter capitalised, so user_name becomes Username and it is exported. The json tag keeps the original key exactly, so decoding still matches your data. Go initialisms such as ID and URL are not special-cased; rename them yourself if your linter asks.

Does it add a package line or imports?

No. The output is type declarations only, so paste it into a file that already has its package clause. Nothing here needs an import either, since every type used is builtin, including interface{} for nulls. Add your own encoding/json import in the file that marshals or unmarshals the structs.

Are there pointers or omitempty tags?

No. Every field is a value type and no tag options are added, so a key missing from the incoming JSON decodes to the zero value rather than to nil, and an empty field is still written out when you marshal. Add pointers or omitempty by hand where you need to tell absent from empty.

When does it refuse to run?

When the document has no object to build a struct from, which gives "top-level value has no object fields to name a type after", and when the input is not valid JSON, which reports the parser message. Documents nested more than 300 levels deep are refused as well.

Related Dev tools

n→nJSON format / validateParse, validate and pretty-print JSON, with a clear parse-error message if it is broken.Runs on each file you dropn→nJSON sort / minify / escapeSort object keys, minify to one line, or escape/unescape JSON for embedding inside another string.Runs on each file you dropn→nJSONPath queryQuery JSON with a JSONPath expression: dot/bracket access, wildcards (*), recursive descent (..). No filter expressions (?()) or slices; those need a full JSONPath engine this build does not have.Runs on each file you dropn→nJSON Schema generatorInfer a draft-07 JSON Schema from an example JSON document. Every observed key is marked required, arrays are typed to accept every observed element.Runs on each file you dropn→nJSON to TypeScriptJSON to TypeScript. Infers types from an example JSON document. Nested objects become named types by their key; mixed-type arrays fall back to a broad/any type.Runs on each file you dropn→nJSON to RustJSON to Rust. Infers types from an example JSON document. Nested objects become named types by their key; mixed-type arrays fall back to a broad/any type.Runs on each file you dropn→nJSON ↔ CSVConvert a flat JSON array of objects to CSV and back. CSV has no type system: JSON->CSV renders numbers/booleans/null as text and CSV->JSON always comes back as strings. A nested object or array field is refused by name rather than silently flattened. CSV parsing is fully quote-aware, embedded newlines included; a file with an unterminated quote is refused rather than guessed at.Runs on each file you dropn→n.env ↔ JSONConvert a .env file to a flat JSON object and back. Values are treated as strings both ways. No nesting, no type coercion of numbers/booleans on the JSON->env direction beyond JSON.stringify.Runs on each file you drop