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
Check these values
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.
Result
How it was worked out
How to use this tool
- 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.
- 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.
- 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. - 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.
- 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
jsontag on every field. You choose if a field that can be null is a pointer (*string), and if a field that can be missing getsomitempty. - 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
valproperties. 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 aCodingKeyslist when a property name is not the same as its key. - Rust: structs for the serde crate, with
Optionfor a field that can be null or missing and arenameattribute 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.0and1e3are 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
intin Go, Java, Kotlin and C#. A larger one gives a 64-bit type. A whole number that does not fit in 64 bits givesjson.Numberin Go andBigIntegerin Java; the other languages get a decimal type and a note, because they have no ready type that keeps every digit. Swift usesIntand Rust usesi64for 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,unknownand so on). TypeScript gets a union such asstring | numberwhen 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
2fagivesN2Fain Go andn2Fain Java. - A name that is a reserved word of the language gets an underscore at its end: the key
classgivesclass_in Java, Kotlin and Swift, andtypegivestype_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
Itemat its end: the keyordersgivesOrdersItem. 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 asString, a number is added. - In C# a property cannot have the name of its own class, so it gets an underscore.
| 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:
typedeclarations 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{}andany,time.Time,json.RawMessage,json.Number,big.Intanduuid.UUID. - In a Go
jsontag the name,-,omitemptyandstringare read. A field with a small first letter is not exported, so it is left out, and the table Fields left out says so.omitemptyis not applied: every key is shown. - Java: classes, records and enums, also inside other classes. Every field that is not
staticortransientgives one key. The key is the field name, or the name in@JsonProperty,@SerializedNameor@Json.@JsonIgnoreleaves 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 withStringkeys,Object,UUID, and dates such asLocalDateandInstant, 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, andenum classandtypealias. Eachvalorvargives one key. The key is the property name, or the name in@SerialName,@SerializedName,@Jsonor@JsonProperty.@Transientand@JsonIgnoreleave a property out. A type with a question mark, such asString?, is a place that can be empty. Starting values are not used. - TypeScript:
interface,typeandenumdeclarations. Properties with the typesstring,number,boolean,null,anyandunknown, lists (T[]andArray<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 withnullorundefined, is a place that can be empty. The properties of the interfaces afterextendscome 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,@JsonFormatand@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
gofmtif 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.