Home › Code and dev › JSON to Code Generator
🔧

JSON to Code Generator

Generate TypeScript, Java, C#, Go, Python and Dart data classes plus JSON Schema from JSON samples, with nullability and type conflict inference

JSON 样本
解析、推断与代码生成都在浏览器本地完成,页面不发任何网络请求。
样本只提供结构线索:字段值不会出现在生成的代码里,字段名与可空性由样本推断。
生成选项
TypeScript
Java
C#
Go
Python
Dart
生成代码
粘贴 JSON 后自动生成代码

About this tool

API docs often say only that the response is JSON, while what you need is a full set of type definitions. Writing them by hand is not hard, but it takes time and invites omissions: dozens of fields, arrays of objects, keys that are null in one sample, and names such as order-id, class or non-ASCII keys that cannot be identifiers. This tool turns inference into visible rules and marks every one of them in the output instead of handing you guessed code.

What it generates

  • TypeScript: interfaces or type aliases, optional fields with ?, and unknown types written as unknown or any
  • Java: entity classes with private fields and accessors, numbers as int, double or BigDecimal, and invalid or reserved key names rewritten with @JsonProperty to keep the original
  • C#: records or classes with PascalCase properties and the original key kept in JsonPropertyName
  • Go: structs with json tags and any types as interface{} for older versions or any for Go 1.18+
  • Python: standard library dataclasses with type hints, or pydantic v2 models using Field(alias=...) to keep original keys
  • Dart: classes with fromJson and toJson, nullable fields as ?, and nested objects split into separate classes
  • JSON Schema: draft-07 with type, required, items and $ref definitions, ready for a validator or another generator

Inference rules

  • Types come from samples: strings, booleans, integers, decimals, null, arrays and objects map to the natural form in each language. When an integer and a decimal appear in the same position, the output uses a decimal and reports it
  • Nullability: fields that were null in any sample become optional or nullable, using the local idiom such as ?, a wrapper type, Optional or a pointer
  • Array merging: fields across objects in one array are unioned. Fields present in every element go into required, partial fields become optional. An empty array cannot reveal an element type, so it becomes an any type with an explicit note
  • Nested classes: arrays of objects and deep objects each become their own type, named from the root type plus the field name, which avoids one giant anonymous structure
  • No forced guesses on conflicts: when a position looks like both a string and an object, the wider any type is used with a message naming both types, telling you which sample is incomplete
  • Legal key names: non-ASCII keys, keys with - or spaces, and reserved words are converted to valid identifiers while the original is kept in an annotation or tag. The keep original names mode instead quotes the key directly
  • Duplicate keys: the last value wins within one object, and the position is reported, because JSON parsers disagree on this case

Getting better types

  • Cover reality in the samples: include a nullable field, a field that only some channels return, an empty array and a non-empty array. Pasting several responses and merging them is more reliable than one large sample
  • When array elements differ a lot, meaning a union type, the tool merges them into a union; splitting them into two types by hand is often clearer
  • For precision sensitive fields such as money, switch the number type to an exact decimal instead of a double
  • Trim large samples: inference still runs beyond 2 MB, but the output becomes huge, so keep a representative excerpt

FAQ

  • How accurate are the generated types? Accuracy depends on the samples. The tool unions what you provide and cannot know about fields you did not paste. Every suspicious inference is reported instead of silently guessing a type
  • Why were fields renamed? The target language rules reject the original key. Go struct fields must start uppercase and Java cannot use class as a field name. The mapping is written into annotations or tags, so serialization still uses the original key
  • Can existing types be kept? Switch the key strategy to keep original names and generate the original spelling, quoting where needed, at the cost of extra syntax in some languages
  • Can several JSON documents be inferred together? Combine multiple responses into an array and paste it; fields across elements are merged, which is the easiest way to cover nullable fields
  • Is the data uploaded? No. Parsing, inference, generation and formatting run in the browser, the page sends no requests, and samples stay in the current tab
  • What is the generated Schema for? It can validate API input with a schema library, feed another code generator, or document the data structure in API docs
  • Copy or download? Copy puts the code on the clipboard for an existing file; download uses the correct extension for each target and suits generating several model files at once
  • What does an empty object warning mean? That object had no keys in the sample, so the generator can only emit a dictionary type. Add a sample with content to get concrete fields

Clean up messy samples with the JSON formatter; convert to configuration arrays with JSON to PHP; export tables with JSON to CSV or JSON to Excel; tidy the generated JS or TS with the JS formatter; test key naming rules with the regex tester; inspect login tokens with the JWT decoder.