JSON Schema to Go: Structs, Pointers and Tags From One Schema

If you already describe your API or config with a JSON Schema, there is no reason to type the matching Go structs by hand. Paste the schema and you get structs that encoding/json can read straight away. To start from a sample document instead of a schema, use JSON to Go; for Rust there is JSON Schema to Rust, and JSON Schema Validator checks a document against the schema itself.

Step 1

Paste or upload your JSON Schema

Drop the schema into the left editor, or upload a .json file. Click Sample if you just want to see what comes out. Set the Struct Name for the top-level type.

Step 2

Read the generated structs

Each object with properties becomes its own struct, $ref targets become named types, and string enums become a named string type with one constant per value. Anything that could not be turned into an exact Go type is listed in a Note comment at the top.

Step 3

Pick the tags you want

The json button adds encoding/json tags (with omitempty on optional fields) and the validate button adds go-playground/validator tags for lengths, ranges, formats like email and enum values. Turn either off if you do not need it.

Step 4

Copy or download the .go file

Copy the code or download it as a .go file and drop it into your package. It is plain Go: only encoding/json is imported, and only when a field needs json.Number.

Frequently Asked Questions

Which JSON Schema keywords does it read?

type (including lists like ["string", "null"]), properties, required, items, enum, const, default, format, minimum and maximum (and the exclusive forms), minLength, maxLength, minItems, maxItems, additionalProperties with a schema, allOf (merged into one struct) and $ref pointing inside the same file, such as #/$defs/Name, #/definitions/Name or # for the root. A oneOf or anyOf that is one type plus null becomes a pointer; anything with several real shapes becomes interface{} and a Note comment says so. References to other files are not followed.

How do required and optional fields come out in Go?

A property listed in required becomes a plain field, like ID int64. A property that is not required, or whose type allows null, becomes a pointer such as *string or *int with omitempty in its json tag, so you can tell a missing value from an empty string or 0. Slices, maps and interface{} fields are never pointers because they can already be nil. A field that refers back to its own struct is always a pointer, since Go does not allow a struct to contain itself by value.

Which integer type does a field get?

int64 by default, so a long ID like 8901240544102066246 fits. A format such as int32 or uint64 is used as written. When both a minimum and a maximum are given (or an enum or const) and they fit in 32 bits, the field is int. A maximum above the int64 range with a minimum of 0 or more gives uint64. The limits are read from the exact digits in your schema, so 18446744073709551615 is not rounded on the way; anything bigger than 64 bits becomes json.Number, which keeps the digits as text.

What do the validate tags check?

They are written for github.com/go-playground/validator/v10: min and max for string lengths, number ranges and array sizes, gt and lt for exclusive limits, email, url, uuid, ipv4, ipv6 and hostname for matching formats, and oneof for enums with simple values. Optional fields start with omitempty so a missing value passes. Required fields do not get the required tag, because validator would then reject a legitimate 0, false or empty string. A pattern is copied into a comment instead of a tag, since validator has no regular-expression check.

What happens to property names that are not valid in Go?

Field names become exported PascalCase with Go initialisms, so user_id becomes UserID. Names starting with a digit or a non-Latin letter get an X prefix (2nd becomes X2nd), and names that clash after cleaning get 2, 3 and so on. The json tag always keeps the original key. encoding/json cannot use a key that is empty or contains a comma, quote, backslash or backtick, so such a field is marked json:"-" and listed in a Note.

Is my schema uploaded anywhere?

No. The conversion runs in your browser, nothing is sent to a server, and nothing is kept: a reload starts with an empty editor.

External Resources & Documentation

JSON Schema Official Website

Complete JSON Schema specification and documentation

go-playground/validator

Go struct validation library using tags compatible with JSON Schema constraints

Understanding JSON Schema

Comprehensive guide to JSON Schema concepts and usage

santhosh-tekuri/jsonschema

JSON Schema validator for Go supporting all drafts

Go encoding/json Package

Official Go JSON encoding and decoding documentation

JSON Schema Implementations

List of JSON Schema validators and tools for various languages

Go Blog: JSON and Go

Official guide to working with JSON in Go

xeipuuv/gojsonschema

Another popular JSON Schema validation library for Go