Convert SQL Schema (CREATE TABLE) to JSON Object

Convert SQL CREATE TABLE statements to JSON schema objects. Perfect for generating API documentation, TypeScript types, and schema visualization.

SQL to JSON Schema Converter Tool

Transform SQL CREATE TABLE statements into structured JSON schemas:

  • Column Definitions: Extract all column names, types, and nullability.
  • Constraint Information: Capture PRIMARY KEY, UNIQUE, NOT NULL, and DEFAULT constraints.
  • Relationship Mapping: Extract foreign key relationships between tables.
  • Type Normalization: Convert database-specific types to standardized JSON schema types.

Example Conversion

Input SQL:

CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  username VARCHAR(50) UNIQUE NOT NULL,
  email VARCHAR(100) UNIQUE NOT NULL,
  age INTEGER,
  is_active BOOLEAN DEFAULT true,
  created_at TIMESTAMP DEFAULT NOW()
);

Output JSON:

{
  "tableName": "users",
  "columns": [
    {
      "name": "id",
      "type": "integer",
      "nullable": false,
      "primaryKey": true,
      "autoIncrement": true
    },
    {
      "name": "username",
      "type": "string",
      "maxLength": 50,
      "nullable": false,
      "unique": true
    },
    {
      "name": "email",
      "type": "string",
      "maxLength": 100,
      "nullable": false,
      "unique": true
    },
    {
      "name": "age",
      "type": "integer",
      "nullable": true
    },
    {
      "name": "is_active",
      "type": "boolean",
      "nullable": false,
      "default": true
    },
    {
      "name": "created_at",
      "type": "datetime",
      "nullable": false,
      "default": "CURRENT_TIMESTAMP"
    }
  ],
  "indexes": [
    { "name": "users_pkey", "columns": ["id"], "unique": true },
    { "name": "users_username_key", "columns": ["username"], "unique": true },
    { "name": "users_email_key", "columns": ["email"], "unique": true }
  ]
}

Common Use Cases

📘 API Documentation

Generate JSON schemas for API documentation. Share database structure with frontend developers in a format they understand.

📝 TypeScript Generation

Use JSON schemas to generate TypeScript interfaces, Zod schemas, or Prisma models for type-safe database access.

🎨 Schema Visualization

Import JSON schemas into schema visualization tools or ER diagram generators for visual documentation.

🔄 Database Migration

Compare JSON schemas to detect schema differences between environments or track schema changes over time.

Advanced Features

🔗 Relationship Extraction

Extracts foreign key relationships and represents them in JSON. Perfect for understanding table dependencies and generating relationship diagrams.

🎯 Type Mapping

Maps SQL types to JSON Schema types: VARCHAR → string, INTEGER → number, BOOLEAN → boolean, etc. Handles database-specific types like JSONB, UUID, and ARRAY.

📋 Constraint Preservation

Preserves all SQL constraints (PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, CHECK) in the JSON representation for complete schema documentation.

🔧 Multiple Table Support

Convert entire database schemas with multiple tables. The JSON output includes all tables and their relationships in a single structured object.

Example: Multiple Tables with Relationships

Input SQL:

CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  name VARCHAR(100)
);

CREATE TABLE posts (
  id SERIAL PRIMARY KEY,
  user_id INT REFERENCES users(id),
  title VARCHAR(200)
);

Output JSON:

{
  "tables": {
    "users": {
      "columns": [
        { "name": "id", "type": "integer", "primaryKey": true },
        { "name": "name", "type": "string", "maxLength": 100 }
      ]
    },
    "posts": {
      "columns": [
        { "name": "id", "type": "integer", "primaryKey": true },
        { "name": "user_id", "type": "integer", "foreignKey": { "table": "users", "column": "id" } },
        { "name": "title", "type": "string", "maxLength": 200 }
      ]
    }
  },
  "relationships": [
    {
      "from": "posts",
      "to": "users",
      "type": "many-to-one",
      "foreignKey": "user_id"
    }
  ]
}

SQL Type to JSON Type Mapping

SQL TypeJSON TypeAdditional Properties
INT, INTEGER, BIGINTinteger-
VARCHAR, TEXT, CHARstringmaxLength (if specified)
BOOLEAN, BOOLboolean-
FLOAT, DOUBLE, DECIMALnumberprecision, scale
DATE, DATETIME, TIMESTAMPdatetimeformat: ISO 8601
JSONB, JSONobjecttype: json
UUIDstringformat: uuid

How to Convert SQL to JSON

  1. 1.
    Paste SQL: Copy your CREATE TABLE statement(s) and paste them into MockBlast's converter.
  2. 2.
    Choose Format: Select JSON Schema format (standard, TypeScript-friendly, or Prisma-compatible).
  3. 3.
    Preview JSON: Review the generated JSON schema and verify all columns and constraints are captured.
  4. 4.
    Download or Copy: Copy JSON to clipboard or download as a .json file for use in your project.

Frequently Asked Questions

Why convert SQL schemas to JSON?
JSON schema representations are useful for API documentation, generating TypeScript interfaces, creating schema visualization tools, and sharing database structures with frontend teams who don't work with SQL directly.
What SQL information is preserved in JSON?
MockBlast extracts column names, data types, constraints (PRIMARY KEY, UNIQUE, NOT NULL, DEFAULT), foreign key relationships, and indexes from your CREATE TABLE statement and represents them in a structured JSON format.
Can I use this to generate TypeScript interfaces?
Yes! The generated JSON schema includes type information that maps to TypeScript types. You can use it as a foundation for generating TypeScript interfaces or Zod schemas for your application.
Does it support all SQL database types?
MockBlast supports PostgreSQL, MySQL, and SQLite CREATE TABLE syntax. It understands database-specific types like PostgreSQL JSONB, MySQL DATETIME, and converts them to normalized JSON representations.
Can I convert multiple tables at once?
Yes! Paste multiple CREATE TABLE statements and MockBlast will generate a JSON object with all table schemas, including their relationships through foreign keys.

Ready to Generate Mock Data?

Stop writing scripts manually. MockBlast generates production-ready seed data for Postgres, MySQL, MongoDB, and JSON in seconds.