Sign up free

JSON to Code

Paste a JSON sample and get matching types in TypeScript, Java, Go, Python, C#, PHP, Kotlin or Swift.

About JSON to Code

JSON to Code saves you from typing out model classes by hand when you start on a new API. Paste a sample response from the API documentation or your browser's network tab, pick a language, and you get TypeScript interfaces, Java classes, Go structs with json tags, Python dataclasses, C# classes with JsonPropertyName attributes, PHP classes with typed properties, Kotlin data classes or Swift Codable structs. If the API is public and you have no sample yet, the HTTP Request Tester can fetch one.

Nested objects become their own named types, and the objects in an array are merged, so a field that appears in only some items is marked optional. You can set the name of the root type. Treat the result as a starting point: a single sample can't show every case, so check types and names against the API documentation. If the sample won't parse, find the problem first with the JSON Formatter. It all runs in your browser, so responses with real data never leave your device.

How to use JSON to Code

  1. 1
    Paste a JSON sample

    Paste an API response or open a .json file.

  2. 2
    Pick a language

    Choose TypeScript, Java, Go, Python, C#, PHP, Kotlin or Swift.

  3. 3
    Name the root type

    Set the name of the top-level class, struct or interface.

  4. 4
    Copy the code

    Copy the generated types or download them.

Why use Cubfile for this

  • Eight languages

    TypeScript, Java, Go, Python, C#, PHP, Kotlin and Swift.

  • Nested types

    Every nested object becomes its own named type.

  • Optional fields

    Objects in arrays are merged, and fields missing from some of them are marked optional.

  • Annotations included

    Go json tags, C# JsonPropertyName attributes and Swift Codable come ready.

FAQ

JSON to Code: questions and answers

How do I convert JSON to a TypeScript interface?
Paste the JSON, choose TypeScript and copy the interfaces. Nested objects get their own interfaces, and fields missing from some array items are marked optional.
Which languages can JSON to Code generate?
TypeScript interfaces, Java classes, Go structs with json tags, Python dataclasses, C# classes with JsonPropertyName, PHP classes with typed properties, Kotlin data classes and Swift Codable structs. The code that sends the request can be generated from a curl command with the cURL to Code Converter.
How are arrays of objects handled?
All objects in an array are merged into one type. A field present in every item stays required, and a field found in only some items is marked optional.
What if my JSON has an error?
The sample has to be valid JSON, so fix it first. The JSON Formatter shows the line and column of the problem, which is usually a missing or extra comma or single quotes.
Is my JSON uploaded?
No. The code is generated in your browser, so customer data or tokens in the sample stay on your device, and there's no daily limit.
Share JSON to Code with a friendIt runs in any browser, and they can try it without signing up.

Related tools

JSON JSON FormatterFormat, validate and minify JSON, errors pointed out by line.
HTML HTML FormatterIndent messy HTML so it’s easy to read.
JS JavaScript FormatterBeautify minified or messy JavaScript.
HEX Color ConverterConvert colors between HEX, RGB, HSL and CMYK, and check contrast.
CRON Cron Expression GeneratorBuild cron expressions and see the next run times in plain words.
CSS CSS FormatterTurn minified CSS into neat, readable rules.
CSS CSS MinifierMinify CSS to load faster.
HTML HTML MinifierShrink HTML by removing comments and extra whitespace.