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.
Paste a JSON sample and get a Zod schema, Rust Serde structs, Kotlin data classes or Swift Codable structs that compile as generated.
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.
null or empty in the sample.| JSON | Zod | Rust | Kotlin | Swift |
|---|---|---|---|---|
| string | z.string() | String | String | String |
| whole number | z.number() | i64 | Long | Int |
| decimal number | z.number() | f64 | Double | Double |
| boolean | z.boolean() | bool | Boolean | Bool |
| null | z.null() | Option<serde_json::Value> | JsonElement? = null | String? |
| array | z.array(…) | Vec<…> | List<…> | […] |
| object | z.object({…}) | pub struct | data class | struct …: 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.
Input:
{"user_id":7,"display-name":"Ada","type":"admin","tags":["admin"],
"address":{"city":"London","geo":{"lat":51.5}},"deleted_at":null}
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.
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.
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.
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.
"tags": [] has no first item, so you get z.array(z.unknown()), Vec<serde_json::Value>, List<JsonElement> or [String]. Put one real element in the sample.[1, "two"] is typed from 1 only. Use z.union, a Rust enum with #[serde(untagged)], or a sealed class..optional(), Option<T> (Serde treats a missing Option field as None), a nullable Kotlin type with = null, or a Swift optional.2fa becomes field_2fa in Rust and field2fa in Kotlin and Swift, mapped back to "2fa".pub type RootList = Vec<RootItem>; in Rust, typealias RootList = List<RootItem> in Kotlin, typealias RootList = [RootItem] in Swift, and z.array(…) in Zod.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.
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.
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.
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.
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.
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.
No. Parsing and code generation run in JavaScript in your browser.