Gratis å bruke, uten konto

JSON-modellgenerator

Lim inn et JSON-eksempel for å lage et utkast til modellklasser i TypeScript, C# eller Java. Arrayer og nestede objekter utledes fra eksempelet; ingenting lastes opp.

Lim inn gyldig JSON på opptil 500 KB. Matriser bruker opptil 300 elementer som utvalg for typeutledning.

Bokstaver, tall og symboler normaliseres til et gyldig typenavn.

Genererte deklarasjoner er anslag basert på utvalget, ikke skjemavalidering eller kode kontrollert av en kompilator. C# og Java bruker objekttyper for blandede unioner; informasjon om valgfrie og nullverdier vises som kommentarer i Java.

Kjører lokalt i denne nettleseren. Ingen konto, opplasting eller API.

Hvordan genererer jeg en modell fra JSON?

Lim inn et JSON-objekt eller en matrise, velg TypeScript, C# eller Java, skriv inn et navn på rotklassen og generer. Kontroller de utledede typene før du bruker koden.

Veiledning

Slik bruker du JSON-modellgenerator

The JSON Model Generator reads a sample of JSON and drafts matching type declarations in TypeScript, C# or Java. You paste an object or an array, pick a language and a root type name, and it works out properties, nested types and lists from what it sees. The result is a starting point built from one sample. It is not a schema, and nothing is compiled or checked for you.

Slik gjør du

  1. Paste valid JSON into the sample box, up to 500 KB. A root object and a root array both work. Load example fills in a ready-made sample if you want to see how it behaves.
  2. Pick TypeScript, C# or Java and type a root type name. Spaces and symbols are turned into a valid name, and the name can be at most 48 characters long.
  3. Press Generate models. The declarations appear below. If you change the language afterwards, they are generated again at once.
  4. Press Copy code, or Download source to save a file named after the root name with a .ts, .cs or .java ending. Read the names and types before you use them.

Eksempler

A list of people in TypeScript

Du skriver inn
Språk
TypeScript
Rottypenavn
Person
JSON
[{"name":null,"score":9.5,"tags":["new","returning"]},{"name":"Ada","active":true,"tags":[]}]
Du får
Kode
// The JSON root is an array; each item matches this interface. export interface Person { name: string | null; score?: number; tags: Array<string>; active?: boolean; }

Name is null in one item and a string in the other, so it becomes string | null. Score and active appear in only one item each, so they are marked optional with a question mark. Tags is a list of strings in both.

Kjørt med verktøyets egen kode.

A list that mixes numbers and text

Du skriver inn
Rottypenavn
Mixed
JSON
{"value":[1,"two"]}
Du får
TypeScript
value: Array<number | string>;
C#
public List<object?> Value { get; set; }
Java
public java.util.List<Object> value;

TypeScript can say number or string. C# and Java have no simple way to do that here, so both fall back to a general object type, and the detail about the two kinds of value is lost.

Kjørt med verktøyets egen kode.

Keys that are not valid names

Du skriver inn
Rottypenavn
my type!
JSON
{"user-name":"x","first name":"y"}
Du får
TypeScript
export interface MyType { "user-name": string; "first name": string; }
Java
public class MyType { public String userName; public String firstName; }

The root name is cleaned to MyType. TypeScript keeps the original keys in quotes. Java renames them to userName and firstName, and the output has no annotation that links these fields back to the original JSON keys.

Kjørt med verktøyets egen kode.

Når det er nyttig

  • Draft the types for a response from an API when you only have an example reply.
  • Get a first version of a C# or Java data class from a JSON file and then refine it by hand.
  • Check which fields of a list of records are sometimes missing or sometimes null.
  • Teach or learn how JSON values map to the types of a typed language.

Begrensninger

  • Input over 500 KB is refused. Only the first 300 items of an array are used to infer types, and a sample with more than 30,000 values or nesting deeper than 24 levels is rejected. An empty array at the root cannot be used.
  • Types come from the values in your sample. A field that is missing from every sample object is not seen, and a field that is always an integer will not be seen as a decimal.
  • Whole numbers become number, long or Long. A field turns into number, double or Double only if some value is not whole, and a value written as 2.0 counts as whole. Dates and other special text stay as plain strings.
  • C# and Java output has no constructors, attributes or annotations. The generated names may not map to the JSON keys by themselves, so add the mapping your own library needs.

Tips

  • Use a sample that includes a record with every field filled in, and one with nulls, so both cases are captured.
  • If two nested objects share a key name, the tool numbers the second type (for example Person2). Rename these after you copy.
  • Paste the generated code into your project and compile it right away. Errors there are the quickest check on the guesses made.

Vanlige spørsmål

How do I generate a model from JSON?

Paste a JSON object or array, choose TypeScript, C# or Java, enter a root type name and press Generate models. Copy the code or download it as a file. Look over the inferred types before you use them in an application.

Is my JSON uploaded or saved?

No. The text is parsed and turned into code in your browser tab. It is not sent to a server and the tool does not store it, so you can paste sample data without leaving your device.

How does it decide that a property is optional or nullable?

A property seen as null in the sample is marked nullable where the language allows it. In a list of objects, a property that is missing from some of them is optional in TypeScript, and shown with a comment in C# and Java.

Does the generated code compile?

The tool does not check that. It writes declarations from your example only. Compile the result in your own project and review names, number ranges, null handling and any syntax your compiler or framework expects.

What happens when a field holds different kinds of values?

TypeScript writes a union such as string | number. C# and Java use the general object type for mixed values, and Java shows null and optional details as comments. The tool cannot say which kind to expect at run time.

What does it do with a root array?

If every item is an object, it builds one type from all of them and adds a comment that the JSON root is a list of that type. A root array of plain values is wrapped in an object with an items property.

Is this the same as a JSON Schema?

No. It makes type declarations for code and cannot check data against rules. A JSON Schema validator does the checking. Use this tool for a first draft of types and the validator for rules like required fields or limits.

Hvordan genererer jeg en modell fra JSON?

Lim inn et JSON-objekt eller en matrise, velg TypeScript, C# eller Java, skriv inn et navn på rotklassen og generer. Kontroller de utledede typene før du bruker koden.

Lastes JSON-dataene mine opp eller lagres de?

Nei. Parsing og generering av modeller utføres i denne nettleserfanen. JSON-dataene dine sendes ikke til en server eller lagres av verktøyet.

Hvordan utledes valgfrie egenskaper og egenskaper som kan være null?

En egenskap som forekommer med null, merkes som nullable der språket støtter det. I en matrise med objekter merkes en egenskap som mangler i noen av utvalgsobjektene som valgfri i TypeScript, eller som merknad i generert C# og Java.

Kontrollerer verktøyet at den genererte koden kompilerer?

Nei. Det utleder bare deklarasjoner fra JSON-eksemplet. Kontroller navn, tallområder, nullhåndtering og generert syntaks i kompilatoren før du bruker koden i et program.

Hva skjer når utvalget inneholder flere verdityper?

TypeScript kan uttrykke unionstyper som string | number. C# og Java bruker object/Object for heterogene unioner; detaljer om null og valgfrie verdier i Java er kommentarer, ikke kjøretidsvalidering.