Generate a Simple CREATE TABLE from JSON
Learn how to convert a flat JSON object into a SQL CREATE TABLE statement with correctly typed columns. Covers the basics of JSON-to-DDL mapping.
Detailed Explanation
From JSON to CREATE TABLE
The simplest conversion takes a flat JSON object and produces a CREATE TABLE statement. Each key becomes a column name, and the JSON value type determines the SQL data type.
Example JSON
{
"id": 1,
"name": "Alice",
"email": "alice@example.com",
"age": 30,
"is_active": true
}
Generated SQL
CREATE TABLE users (
id INTEGER NOT NULL,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
age INTEGER NOT NULL,
is_active BOOLEAN NOT NULL
);
Type Mapping Rules
The converter applies the following default mappings:
| JSON type | SQL type |
|---|---|
| number (integer) | INTEGER |
| number (float) | DECIMAL or DOUBLE PRECISION |
| string | VARCHAR(255) |
| boolean | BOOLEAN |
| null | inferred from context or TEXT |
Table Naming
By default the converter derives the table name from the root key or uses a generic name like my_table. You can override it in the tool options. Best practice is to use snake_case and plural nouns for table names (e.g., users, order_items).
Column Naming
JSON keys are converted to snake_case if they are in camelCase. For example, firstName becomes first_name. This follows the SQL convention used by PostgreSQL, MySQL, and most ORMs.
A simple flat object is the foundation for all other conversions. Once you understand this basic mapping, you can move on to handling nested objects, arrays, and constraints.
Use Case
You have a sample JSON record from an API response and want to quickly scaffold a database table that can store that data, without manually writing DDL.
Try It — JSON to SQL Schema
Related Topics
Map JSON Numbers to SQL Integer and Numeric Columns
Basic Tables
Convert JSON Strings to VARCHAR and TEXT Columns
Basic Tables
Map JSON Booleans to SQL Boolean and Bit Columns
Column Types
Generate SQL Primary Key Constraints from JSON
Constraints
Generate a Complete User Table Schema from JSON
Real-World Schemas