JSON to Zod, Rust, Kotlin and Swift Code Generator

Paste a JSON sample and get a Zod schema, Rust Serde structs, Kotlin data classes or Swift Codable structs that compile as generated.

Generate code from a JSON sample

This JSON code generator reads one JSON sample and writes ready-to-use types for four targets: a Zod schema for TypeScript validation, Rust structs with Serde derives, Kotlin data classes for kotlinx.serialization, or Swift structs that conform to Codable. Every nested object becomes its own named type, arrays become typed lists, and keys that are not valid identifiers keep working through rename attributes. Everything runs in your browser, so private API responses stay on your device.

The Rust, Kotlin and Swift outputs compile as generated: types are declared before they are used, the imports are included, and reserved words such as type or class are escaped. One sample still cannot tell you which fields are optional or what a null really holds, so the sections below show what to review.

How to use it

  1. Choose the Target language: Zod, Rust (Serde), Kotlin or Swift.
  2. Paste a representative JSON object into Input JSON. Use a real response with every optional field filled in, not a minimal one.
  3. Press Generate. Invalid JSON shows the parser error instead of code.
  4. Press Copy, paste into your project, and replace any placeholder types for fields that were null or empty in the sample.

Type mapping

JSONZodRustKotlinSwift
stringz.string()StringStringString
whole numberz.number()i64LongInt
decimal numberz.number()f64DoubleDouble
booleanz.boolean()boolBooleanBool
nullz.null()Option<serde_json::Value>JsonElement? = nullString?
arrayz.array(…)Vec<…>List<…>[…]
objectz.object({…})pub structdata classstruct …: Codable

Arrays take their element type from the first item. Nested objects are named in PascalCase after their key, so address becomes Address and the items of orders become OrdersItem; if two keys would give the same name, the second gets a number.

Worked example

Input:

{"user_id":7,"display-name":"Ada","type":"admin","tags":["admin"],
 "address":{"city":"London","geo":{"lat":51.5}},"deleted_at":null}

Zod output

z.object({
  "user_id": z.number(),
  "display-name": z.string(),
  "type": z.string(),
  "tags": z.array(z.string()),
  "address": z.object({
    "city": z.string(),
    "geo": z.object({
      "lat": z.number()
    })
  }),
  "deleted_at": z.null()
})

Finish it with import { z } from "zod"; and export const User = z.object({…});, then type User = z.infer<typeof User>. Tighten user_id to z.number().int() and change deleted_at to z.string().nullable(), since a field that is null in the sample is usually a string or date when set.

Rust (Serde) output

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Geo {
    pub lat: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Address {
    pub city: String,
    pub geo: Geo,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Root {
    pub user_id: i64,
    #[serde(rename = "display-name")]
    pub display_name: String,
    pub r#type: String,
    pub tags: Vec<String>,
    pub address: Address,
    pub deleted_at: Option<serde_json::Value>,
}

Add serde = { version = "1", features = ["derive"] } and serde_json = "1" to Cargo.toml, then let user: Root = serde_json::from_str(&text)?;. The display-name key is read through #[serde(rename)], and type, a Rust keyword, is written as the raw identifier r#type, which Serde maps back to "type". Change deleted_at to Option<String> once you know what it holds.

Kotlin output

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement

@Serializable
data class Geo(
    val lat: Double
)

@Serializable
data class Address(
    val city: String,
    val geo: Geo
)

@Serializable
data class Root(
    @SerialName("user_id") val userId: Long,
    @SerialName("display-name") val displayName: String,
    val type: String,
    val tags: List<String>,
    val address: Address,
    @SerialName("deleted_at") val deletedAt: JsonElement? = null
)

Apply the kotlin("plugin.serialization") Gradle plugin, add kotlinx-serialization-json, and decode with Json.decodeFromString<Root>(text). Snake_case and dashed keys are mapped with @SerialName. Fields that were null default to null, so the key may also be missing. For other missing keys, add = null with a nullable type or create the decoder with Json { ignoreUnknownKeys = true } for extra keys.

Swift output

import Foundation

struct Geo: Codable {
    let lat: Double
}

struct Address: Codable {
    let city: String
    let geo: Geo
}

struct Root: Codable {
    let userId: Int
    let displayName: String
    let type: String
    let tags: [String]
    let address: Address
    let deletedAt: String?  // null in sample: set the real type

    enum CodingKeys: String, CodingKey {
        case userId = "user_id"
        case displayName = "display-name"
        case type
        case tags
        case address
        case deletedAt = "deleted_at"
    }
}

Decode with try JSONDecoder().decode(Root.self, from: data). The CodingKeys enum is only written when at least one key was renamed, and then it lists every property, as Swift requires. A field that was null is typed String? with a comment, because Swift has no built-in "any JSON" type: replace it with the real type. Optional properties decode both a missing key and null.

Edge cases to check

Other languages and related tools

If you work in TypeScript, the JSON to TypeScript page can also emit a Zod schema, and it merges every item of an array of objects so keys missing from some items come out as .optional(). For other languages, use the dedicated generators: JSON to Go, JSON to C#, JSON to Java, JSON to Python and JSON to Dart. For Python validation models there is JSON to Pydantic, and if you need a language-neutral contract rather than code, start from the JSON Schema generator.

Frequently asked questions

Does the Rust, Kotlin and Swift output compile?

Yes. Each nested object becomes its own top-level type, written before the type that uses it, with the imports it needs. We test the Rust output with serde and serde_json and the Swift output with JSONDecoder, decoding the original sample and encoding it back with the original key names.

How are keys like display-name, user_id or type handled?

Keys are converted to each language's naming style (snake_case for Rust, camelCase for Kotlin and Swift) and the original JSON key is kept with #[serde(rename)], @SerialName or a CodingKeys enum. Reserved words are escaped: r#type in Rust, backticks in Kotlin and Swift. Keys that start with a digit get a field prefix.

Are integers and decimals told apart?

Yes for Rust, Kotlin and Swift: whole numbers become i64, Long or Int, numbers with a fraction become f64 or Double. Zod uses z.number(); add .int() where you need it. If a field is whole in your sample but can have decimals elsewhere, switch it to the floating-point type.

What happens with null values and empty arrays?

A null becomes Option<serde_json::Value> in Rust, JsonElement? = null in Kotlin and an optional String? in Swift with a comment, because the sample cannot show the real type. An empty array becomes Vec<serde_json::Value>, List<JsonElement>, [String] or z.array(z.unknown()). Replace these with the real types.

Does it handle arrays of objects with different keys?

No. The item type comes from the first array element. If later items have extra keys, add them by hand, or use JSON to TypeScript or JSON to Go, which merge all items.

Is my JSON sent to a server?

No. Parsing and code generation run in JavaScript in your browser.