cd .. (all tools)

JSON → TypeScript

Types and a Zod schema from a JSON example.

// Runs in your browser. Nothing you type here is sent anywhere.

The types come from this one example: a field that is missing or null here may be something else in other data.

TypeScript

export interface User {
  id: number;
  name: string;
  email: null;
  roles: string[];
  address: Address;
  orders: Order[];
}

export interface Order {
  id: number;
  total: number;
  paid: boolean;
  coupon?: string;
}

export interface Address {
  city: string;
  country: string;
}

Zod schema

import { z } from 'zod';

const addressSchema = z.object({
  city: z.string(),
  country: z.string(),
});

const orderSchema = z.object({
  id: z.number(),
  total: z.number(),
  paid: z.boolean(),
  coupon: z.string().optional(),
});

export const userSchema = z.object({
  id: z.number(),
  name: z.string(),
  email: z.null(),
  roles: z.array(z.string()),
  address: addressSchema,
  orders: z.array(orderSchema),
});

export type User = z.infer<typeof userSchema>;