Back to blog

Generate better types with multiple JSON samples

quicktype can analyze multiple JSON samples to generate more accurate types. Learn how to use this feature for better type inference.

quicktype's type inference improves dramatically when you provide multiple JSON samples. This post explains why and how to use this feature.

The Problem with Single Samples

A single JSON sample might not represent all possible values:

// Sample 1: User is online
{
  "name": "Alice",
  "status": "online",
  "lastSeen": null
}

From this, quicktype infers lastSeen is always null. But in reality:

// Sample 2: User is offline
{
  "name": "Bob",
  "status": "offline",
  "lastSeen": "2018-03-08T10:30:00Z"
}

Multiple Samples to the Rescue

Give quicktype both samples, and it correctly infers:

interface User {
    name: string;
    status: string;
    lastSeen: string | null;
}

Using Multiple Samples

Command Line

quicktype sample1.json sample2.json sample3.json -o Types.ts

Web App

  1. Go to app.quicktype.io
  2. Paste your first JSON sample
  3. Click "Add sample" (+ button)
  4. Paste additional samples

Best Practices

  • Include edge cases: Empty arrays, null values, optional fields
  • Cover all variants: Different enum values, union types
  • Use real data: Production API responses are ideal

How It Works

quicktype merges the type graphs from all samples, creating unions where values differ. This produces more robust types that handle real-world data variations.