JSON to SQL Converter

Transform JSON data into SQL CREATE TABLE and INSERT statements

JSON Input

SQL Output

About JSON to SQL Converter

Convert JSON data to SQL statements with automatic CREATE TABLE generation and INSERT statements. Supports MySQL, PostgreSQL, SQLite, and SQL Server dialects.

Key Features

  • Multiple SQL Dialects: MySQL, PostgreSQL, SQLite, and SQL Server support
  • CREATE TABLE Generation: Automatically generates table schema from JSON structure
  • Type Inference: Intelligently infers SQL data types from JSON values
  • INSERT Statements: Generates INSERT statements for all data rows
  • Primary Key Support: Configurable primary key with auto-increment
  • DROP TABLE Option: Optional DROP TABLE IF EXISTS statement
  • Custom Table Names: Specify your preferred table name
  • File Upload: Upload JSON files directly or paste data

How to Use

  1. Input JSON Data: Paste your JSON array or upload a .json file
  2. Configure Options: Select SQL dialect, table name, and primary key settings
  3. Review Output: The SQL statements update automatically
  4. Copy or Download: Use the Copy or Download button to save your .sql file
  5. Execute in Database: Run the SQL in your database management tool

Example Conversion

JSON Input:

[
  {
    "name": "John Doe",
    "email": "john@example.com",
    "age": 28,
    "active": true
  },
  {
    "name": "Jane Smith",
    "email": "jane@example.com",
    "age": 32,
    "active": true
  }
]

SQL Output (MySQL):

CREATE TABLE my_table (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  email VARCHAR(255) NOT NULL,
  age INT NOT NULL,
  active TINYINT(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO my_table (name, email, age, active) VALUES ('John Doe', 'john@example.com', 28, 1);
INSERT INTO my_table (name, email, age, active) VALUES ('Jane Smith', 'jane@example.com', 32, 1);

SQL Dialect Differences

  • MySQL: Uses AUTO_INCREMENT, TINYINT for booleans, InnoDB engine
  • PostgreSQL: Uses SERIAL, BOOLEAN type, multi-row INSERT optimization
  • SQLite: Uses AUTOINCREMENT, INTEGER for booleans, simplified syntax
  • SQL Server: Uses IDENTITY, BIT for booleans, T-SQL syntax

Data Type Mapping

  • String: JSON strings → VARCHAR(255) or TEXT for long strings
  • Integer: JSON integers → INT
  • Float: JSON decimals → DOUBLE or DOUBLE PRECISION
  • Boolean: JSON booleans → BOOLEAN, TINYINT(1), or BIT
  • Date: ISO date strings → DATE or DATETIME
  • Object/Array: JSON objects/arrays → JSON or JSONB
  • Null: JSON null → NULL

Common Use Cases

  • Data Migration: Import JSON data into SQL databases
  • API to Database: Convert API responses to database tables
  • Database Seeding: Generate seed data for development/testing
  • Schema Generation: Create database schemas from JSON structure
  • Data Import: Bulk import JSON data into existing databases
  • Testing: Generate test data for SQL databases

Type Inference Rules

  • Email Detection: Strings matching email pattern → VARCHAR(255)
  • Date Detection: ISO date strings → DATE or DATETIME
  • Long Strings: Strings over 255 characters → TEXT
  • Primary Key: Fields named 'id' or matching primary key → AUTO_INCREMENT
  • JSON Fields: Nested objects/arrays → JSON or JSONB column

Best Practices

  • Consistent Data: Ensure all JSON objects have the same structure
  • Choose Dialect: Select the correct SQL dialect for your database
  • Review Types: Verify inferred data types match your requirements
  • Test First: Test generated SQL on a development database first
  • Backup Data: Always backup before running DROP TABLE statements
  • Validate JSON: Ensure JSON is valid before conversion

PostgreSQL Optimizations

  • Multi-row INSERT: Uses single INSERT with multiple value sets
  • SERIAL Type: PostgreSQL-native auto-increment type
  • JSONB Support: Uses JSONB for better performance with JSON data
  • CASCADE: Includes CASCADE in DROP TABLE for foreign keys

Executing Generated SQL

You can execute the generated SQL in various ways:

# MySQL
mysql -u username -p database_name < my_table.sql

# PostgreSQL
psql -U username -d database_name -f my_table.sql

# SQLite
sqlite3 database.db < my_table.sql

Privacy & Security

All conversions happen locally in your browser. Your JSON data is never uploaded to any server, ensuring complete privacy and security.