Toolvito

Works on this device

Data Model Code Generator

Paste a sample of your JSON data and get the type definitions for it in your programming language. The tool can also read Go structs, Java classes, Kotlin classes or TypeScript interfaces and write a JSON sample for them. Your text is read in this browser. It is not uploaded and it is not run.

Your data

Writes a Go struct with json tags for each object in the sample.

Type or paste an example of your JSON data: one object, or a list of objects. It is read on this device and is not uploaded.

Use letters and digits, such as Order or OrderList. Other types get their names from the keys.

A plain Go string cannot hold null. A pointer can.

Your values are worked out in this browser. They are not sent to a server.

Your result will show here

Type or paste your text, or choose Try an example.

How to use this tool

  1. Choose what you want to make: code for one language from a JSON sample, or a JSON sample from Go, Java, Kotlin or TypeScript code.
  2. Type or paste your text in the box, or open a file from your device. The result updates as you type. Try an example fills the box with a small example.
  3. For code from JSON, type a name for the main type, such as Order. Then choose the options for your language, for example the JSON library that the annotations are for.
  4. Read the table How each key was read. It shows the name and the type of every field, and what the tool saw in the sample.
  5. Choose Copy text or Download file to keep the result. Download CSV saves the counts and the table.

What you get for each language

  • Go: one struct for each object, with a json tag on every field. You choose if a field that can be null is a pointer (*string), and if a field that can be missing gets omitempty.
  • Java: classes with private fields, getters and setters, or records. You choose annotations for Jackson (@JsonProperty), for Gson (@SerializedName), or none.
  • Kotlin: data classes with val properties. You choose annotations for kotlinx.serialization (@SerialName), Gson (@SerializedName), Moshi (@Json), or none.
  • TypeScript: interfaces or type aliases. A key that can be missing gets ?, and a value that can be null gets | null.
  • C#: classes with properties and an attribute for System.Text.Json (JsonPropertyName) or Json.NET (JsonProperty).
  • Swift: structs that follow Codable, with a CodingKeys list when a property name is not the same as its key.
  • Rust: structs for the serde crate, with Option for a field that can be null or missing and a rename attribute when the names differ.
  • In every language the key of the sample is kept in the tag, annotation or attribute. So a JSON library reads and writes the same keys, even when the field name in the code is different.

How the types are worked out from the sample

  • Text gives a text type, true or false gives a boolean type, and an object gives a type of its own with one field for each key.
  • A number with no decimal point and no exponent is a whole number. 1.0 and 1e3 are decimal numbers. If a key has whole numbers in some places and decimal numbers in others, it is a decimal number.
  • A whole number that fits in 32 bits gives int in Go, Java, Kotlin and C#. A larger one gives a 64-bit type. A whole number that does not fit in 64 bits gives json.Number in Go and BigInteger in Java; the other languages get a decimal type and a note, because they have no ready type that keeps every digit. Swift uses Int and Rust uses i64 for every whole number that fits in 64 bits.
  • All the items of a list are read together. For a list of objects, a key that is missing in some items can be missing, and a key that is null in some items can be null. The same holds when the sample itself is a list of objects.
  • Two objects with the same keys, the same kind of value for each key and the same "can be missing" and "can be null" marks share one type. The order of the keys does not matter. The type gets its name from the first key where the shape is met.
  • If a key holds values of different kinds, such as text in one item and a number in another, its type is the "any value" type of the language (any, Object, unknown and so on). TypeScript gets a union such as string | number when the kinds are all text, number or true/false.
  • A key that is only ever null, and a list that is always empty, give no information. They also get the "any value" type, and a note names these fields.
  • If a key comes twice in one object, the last value is used, as most programs do.

How names are made

A key is cut into words. Only the letters A to Z and the digits 0 to 9 are used: every other character ends a word. A new word also starts at a capital letter after a small letter (userId is user + Id), at the last capital of a run of capitals (HTTPServer is HTTP + Server), and after digits (sha256sum is sha256 + sum). Then the words are joined in the style of the language.

  • A key with no letter from A to Z and no digit gives the name field.
  • A name that starts with a digit gets the letter N in front: the key 2fa gives N2Fa in Go and n2Fa in Java.
  • A name that is a reserved word of the language gets an underscore at its end: the key class gives class_ in Java, Kotlin and Swift, and type gives type_ in Rust.
  • If two keys of one object give the same name, the second gets a 2 at its end, the third a 3, and so on.
  • A type gets the name of its key in PascalCase. The type of the items of a list gets Item at its end: the key orders gives OrdersItem. The tool does not try to make a word singular. If the name is taken, or is the name of a built-in type such as String, a number is added.
  • In C# a property cannot have the name of its own class, so it gets an underscore.
Names in each language for the key user_id
Language Field name Style Where the key is kept
Go UserID PascalCase, with words such as ID, URL and HTTP in capitals json tag
Java userId camelCase @JsonProperty or @SerializedName
Kotlin userId camelCase @SerialName, @SerializedName or @Json
TypeScript user_id the key itself, in quotes if it is not a plain name the property name
C# UserId PascalCase JsonPropertyName or JsonProperty
Swift userId camelCase CodingKeys
Rust user_id snake_case serde rename, when the names differ

From code to a JSON sample

The tool reads type declarations and writes a JSON text with a placeholder in each place: "" for text, 0 for a number, false for true or false, and null where any kind of value is allowed. You can choose to see one item in each list, or empty lists and null in each place that can be empty.

  • Go: type declarations of structs and of other types, in a group or alone. Fields with the basic types, pointers, slices, arrays with a plain number as length, maps with text or whole-number keys, structs inside structs, interface{} and any, time.Time, json.RawMessage, json.Number, big.Int and uuid.UUID.
  • In a Go json tag the name, -, omitempty and string are read. A field with a small first letter is not exported, so it is left out, and the table Fields left out says so. omitempty is not applied: every key is shown.
  • Java: classes, records and enums, also inside other classes. Every field that is not static or transient gives one key. The key is the field name, or the name in @JsonProperty, @SerializedName or @Json. @JsonIgnore leaves a field out. The fields of a parent class come first.
  • Java types that are read: the basic types and their object forms, String, BigDecimal, BigInteger, lists and sets, arrays, maps with String keys, Object, UUID, and dates such as LocalDate and Instant, which are shown as text in the ISO 8601 form. An enum is shown as the name of its first value.
  • In Java, methods, constructors and comments are passed over. Getters do not add or rename keys.
  • Kotlin: classes with their properties in the round brackets after the name, such as data class Order(val id: Int), also inside other classes, and enum class and typealias. Each val or var gives one key. The key is the property name, or the name in @SerialName, @SerializedName, @Json or @JsonProperty. @Transient and @JsonIgnore leave a property out. A type with a question mark, such as String?, is a place that can be empty. Starting values are not used.
  • TypeScript: interface, type and enum declarations. Properties with the types string, number, boolean, null, any and unknown, lists (T[] and Array<T>), tuples, Record<string, T>, object types inside a type, and names of other declared types. A text or a whole number as a type is shown as it is. For a union the first type is shown. A property with ?, and a union with null or undefined, is a place that can be empty. The properties of the interfaces after extends come first.
  • Some forms are refused with their line number, so that nothing is read wrongly without a word. Go: embedded fields and type parameters. Java: type parameters, Optional, byte[] and interfaces as field types. Kotlin: a property in the body of a class, a parent class, a sealed class and function types. TypeScript: methods and function types, type parameters, & types, mapped types and classes. In Java and Kotlin: annotations that change the JSON, such as @JsonUnwrapped, @JsonNaming, @JsonFormat and @Contextual. In every language: a type that is not declared in the text.
  • A type that holds itself is written once. Where it comes again, the place is null, or the list is empty.

Worked example

The sample [{"a": 1, "b": "x"}, {"a": 2.5, "b": null, "c": [1, 2]}] is a list of two objects, so one type is written. The key a is 1 in one item and 2.5 in the other, so it is a decimal number. The key b is null in one item, so it can be null. The key c is missing in the first item, so it can be missing.

For Go, with the starting options, the struct Root has the fields A float64, B *string and C []int. Their tags are json:"a", json:"b" and json:"c,omitempty", each between backquotes and lined up in one column. For TypeScript the interface Root has a: number;, b: string | null; and c?: number[];. For Kotlin with kotlinx.serialization the last property is @SerialName("c") val c: List<Int>? = null.

In the other direction, the Go code type Item struct { SKU string `json:"sku"`; Tags []string `json:"tags"`; Note *string `json:"note"` } gives a sample with the keys sku, tags and note. With one item in each list the values are "", a list with one "", and "". With empty lists they are "", [] and null.

Limits you should know

  • The types come from your sample only. A key that is never missing in the sample can be missing in other data. A whole number in the sample can be a decimal number, or a larger number, in other data. Check the code against your real data.
  • The code is written by rules and was not compiled by this page. Check it in your own project. For Go, run gofmt if you change the fields.
  • Every object becomes a type with fixed fields. The tool does not guess that an object is a map with free keys, and it does not guess dates, enums or other special kinds of text: text is text.
  • Letters outside A to Z, such as accented letters, are not used in names. The key itself is still kept in the tag or annotation.
  • Go cannot name some keys in a struct tag: an empty key, or a key with a comma, a quote or a backslash. Such a field gets the tag json:"-" and a comment.
  • The JSON sample can have up to 1,000,000 characters and can be nested up to 200 levels deep. Code can have up to 200,000 characters, with types inside types up to 100 levels. A sample made from code can be nested up to 50 levels and can have up to 20,000 values.
  • In the other direction Go, Java, Kotlin and TypeScript are read, and only the forms listed above. C#, Swift, Rust and Dart code, JSON Schema and XML Schema (XSD) are not read by this page.
  • The sample has fixed placeholder values. The page does not make up data such as names, dates or prices.
  • The page does not explain how to call a JSON library in your program, and it does not explain error messages of such a library.
  • A JSON file from an address on the web cannot be opened here. Copy the text and paste it, or save the file and open it.

Related tools