What it does
JSON to Code generates typed models from a sample of JSON: interfaces and classes for TypeScript, C#, Java, Python, Go, Kotlin, Swift, Rust, PHP and Dart, or a JSON Schema. Nested objects become their own types and arrays become typed lists, so you can paste an API response and have models ready to use.
How to use it
- Paste JSON, or click Load sample.
- Choose the Language. Your choice is remembered.
- Set the Root type name, for example
UserorOrderResponse. - Copy or download the generated code. It updates as you edit the JSON.
Example
{ "id": 7, "name": "Ada", "tags": ["math"], "address": { "city": "London", "zip_code": "EC1A" } }
TypeScript, with root type User:
export interface User {
id: number;
name: string;
tags: string[];
address: Address;
}
export interface Address {
city: string;
zip_code: string;
}
C# (System.Text.Json):
public class User
{
[JsonPropertyName("id")]
public long Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; } = default!;
[JsonPropertyName("tags")]
public List<string> Tags { get; set; } = default!;
[JsonPropertyName("address")]
public Address Address { get; set; } = default!;
}
Good to know
- Types are inferred from the sample. A field that is
nullin the sample cannot be typed, so give it a real value if you can. - When an array holds objects with different fields, fields missing from some items become optional.
- Property names follow each language's convention (PascalCase in C#, snake_case in Python) and keep the original JSON name in an attribute or tag.
- Escaped (stringified) JSON is unescaped first.
FAQ
Which C# serializer is used?
System.Text.Json attributes (JsonPropertyName). The classes also work with Newtonsoft.Json if you change the attribute.
Can I generate classes from XML instead? Yes, with XML to Code.