JSON to SQL: CREATE TABLE, INSERT and How the Dialects Differ
Last updated: · 8 min read
Getting JSON records from an API or an export file into a database takes two things: a CREATE TABLE statement that declares the columns with sensible types, and INSERT statements that add each record. The JSON to SQL Converter produces both. It picks column types by looking at every record and quotes identifiers and strings according to the dialect you choose. The dialect matters, because PostgreSQL, MySQL, SQLite and SQL Server write booleans, long text and identifiers differently. This guide explains the type inference rules, the differences between the four dialects, and what you still have to add by hand before the generated table is ready for real use. The output shown is the tool's real result, and the conversion runs in your browser.
Formula
Rules:
1. The input is an array of objects or a single object. Each object is a row and each key a column; columns keep the order in which they first appear.
2. A column's type is chosen by looking at every row:
integer (32-bit) → INTEGER (PostgreSQL, SQLite), INT (MySQL, SQL Server)
integer (64-bit) → BIGINT (INTEGER in SQLite)
decimal number → DOUBLE PRECISION, DOUBLE, REAL, FLOAT
true / false → BOOLEAN (PostgreSQL, MySQL), INTEGER (SQLite), BIT (SQL Server)
string → TEXT (PostgreSQL, SQLite), VARCHAR(255) (MySQL), NVARCHAR(255) (SQL Server)
object or array → JSONB, JSON, TEXT, NVARCHAR(MAX)
3. A column with mixed types, or one that was only ever null, becomes a text column and its values are written as text.
4. A column filled in every row gets NOT NULL; a missing key and a null value are both inserted as NULL.
5. Identifiers are quoted per dialect: "name" (PostgreSQL, SQLite), `name` (MySQL), [name] (SQL Server).
6. Single quotes inside strings are doubled. MySQL also gets its backslashes doubled; SQL Server strings are written as N'…'.
7. Numbers are copied exactly as the JSON spells them. Integers too large for 64 bits go into a NUMERIC, DECIMAL(65,0) or DECIMAL(38,0) column, or into a text column if even that is too small.
8. One INSERT is produced per record. No PRIMARY KEY, index or foreign key is written.
Input (table name: items, dialect: PostgreSQL):
[{"id":1,"name":"Ada","email":"ada@example.com","score":9.5,"active":true},{"id":2,"name":"O'Brien","email":null,"score":7,"active":false}]
Output:
CREATE TABLE "items" (
"id" INTEGER NOT NULL,
"name" TEXT NOT NULL,
"email" TEXT,
"score" DOUBLE PRECISION NOT NULL,
"active" BOOLEAN NOT NULL
);
INSERT INTO "items" ("id", "name", "email", "score", "active") VALUES (1, 'Ada', 'ada@example.com', 9.5, TRUE);
INSERT INTO "items" ("id", "name", "email", "score", "active") VALUES (2, 'O''Brien', NULL, 7, FALSE);This is a type inference and formatting algorithm, not a formula. The tool first walks all records and collects which JSON types occur in each column, then picks the matching type of the dialect. In MySQL a text column grows with its longest value: VARCHAR(255) up to 255 characters, TEXT up to 16,383 characters and LONGTEXT beyond that; in SQL Server, strings longer than 255 characters become NVARCHAR(MAX). A table name may contain only letters, digits and underscores, may be at most 63 characters long and may be written as schema.table.
What the inputs mean
- JSON
- An array of objects or a single object. An empty array, an array of numbers or strings, and objects without any fields are rejected. The more records you supply, the better the column types and NOT NULL decisions.
- Table name
- The table name used in the statements; items is used when the box is empty. It must start with a letter or underscore and contain only letters, digits and underscores. A schema-qualified name such as public.orders is accepted.
- SQL dialect
- PostgreSQL (default), MySQL / MariaDB, SQLite or SQL Server. The choice decides the column types, how identifiers are quoted and how booleans and strings are written.
Worked example: Converting two records for four dialects
- The input has two records; the table name is items and the dialect PostgreSQL. id is a small integer in both records and always present: "id" INTEGER NOT NULL,
- score is 9.5 in one record and 7 in the other; because a decimal value occurs, the column becomes "score" DOUBLE PRECISION NOT NULL, The value 7 is written in the statement as it is.
- email is null in the second record, so the column gets no NOT NULL: "email" TEXT, In the second row the value is written as NULL.
- The single quote in O'Brien is doubled; otherwise the string would end early and break the statement: INSERT INTO "items" ("id", "name", "email", "score", "active") VALUES (2, 'O''Brien', NULL, 7, FALSE);
- For MySQL the same input starts with CREATE TABLE `items` ( Identifiers are in backticks and the text column becomes `name` VARCHAR(255) NOT NULL,
- SQLite has no separate boolean type; the active column becomes "active" INTEGER NOT NULL and the values are written as 1/0: INSERT INTO "items" ("id", "name", "email", "score", "active") VALUES (1, 'Ada', 'ada@example.com', 9.5, 1);
- In SQL Server identifiers are in square brackets and strings carry the N prefix; the active column becomes [active] BIT NOT NULL and the second row reads INSERT INTO [items] ([id], [name], [email], [score], [active]) VALUES (2, N'O''Brien', NULL, 7, 0);
- Nested and mixed values: for PostgreSQL, [{"id":1,"meta":{"k":[1,2]},"big":9007199254740993,"code":"007"},{"id":2,"code":7}] produces "meta" JSONB, for meta and "code" TEXT NOT NULL for code. code is a string in one record and a number in the other, so the column is text and the leading zeros of 007 survive: INSERT INTO "items" ("id", "meta", "big", "code") VALUES (1, '{"k":[1,2]}', 9007199254740993, '007');
- The value 9007199254740993 in that row was copied digit for digit. It is larger than 2^53; converted to a floating-point number it would have become 9007199254740992.
Result: The PostgreSQL output consists of a CREATE TABLE with five columns and two INSERT statements. Before running it, add a primary key, change decimal columns that hold money to an exact type (NUMERIC or DECIMAL) and review the NOT NULL decisions.
🗄️ JSON to SQL Converter (CREATE + INSERT)
Free, no sign-up; your data stays in your browser.
Open the calculator →How to read the result
The generated table is the simplest one that fits the sample; the design decisions remain yours. The tool makes no column a primary key and adds no indexes or foreign keys. NOT NULL rests only on the records you supplied: a field filled in every record today may arrive empty tomorrow. Decimal numbers go into binary floating-point types, which store approximations, so amounts and rates should be changed to NUMERIC or DECIMAL. Always quoting identifiers has a consequence in PostgreSQL: unquoted names are folded to lower case while quoted names are case-sensitive, so a column created as "userName" must be quoted in your queries too. The output is meant for one-off loads and test data. In application code, use parameterised queries instead of putting values into the SQL text.
Common mistakes
- Running the statements against the wrong dialect. Backtick-quoted names are a syntax error in PostgreSQL, and names in square brackets are one in MySQL; pick the target database and generate again.
- Forgetting the primary key. The tool writes the id column as INTEGER NOT NULL and nothing more; add PRIMARY KEY, uniqueness and auto-increment definitions by hand.
- Leaving money in a DOUBLE PRECISION, DOUBLE, REAL or FLOAT column. Those types store approximate values; choose an exact type such as NUMERIC(12,2) for amounts.
- Accepting NOT NULL without question. The tool only looks at the records you gave it; a field that is filled in a two-record sample may be empty in real data, and the insert then fails.
- Sending codes with significant leading zeros as numbers. A postcode written as 7 in the JSON ends up in an INTEGER column; keep values such as "007" as strings in the JSON.
- Querying mixed-case column names without quotes in PostgreSQL. The tool creates names in quotes; SELECT userName is folded to username and the column is not found.
- Running a very large array in one go without a transaction. Each record is its own INSERT; wrapping thousands of rows between BEGIN and COMMIT speeds the load up considerably and lets you roll back a load that stops halfway.
Frequently asked questions
Should text columns be VARCHAR or TEXT?
It depends on the dialect. The PostgreSQL documentation states that there is no performance difference among char(n), varchar(n) and text, which is why the tool writes TEXT for PostgreSQL and SQLite. For MySQL it uses VARCHAR(255) up to 255 characters and TEXT or LONGTEXT for longer values; for SQL Server NVARCHAR(255) or NVARCHAR(MAX). You can tighten the length limit to suit your own data.
Why do true and false become 1 and 0 in some dialects?
SQLite has no separate boolean storage class; the values are stored as the integers 0 and 1. In SQL Server the matching type is BIT. In MySQL, BOOLEAN is a synonym for TINYINT(1), and TRUE and FALSE are merely aliases for 1 and 0. PostgreSQL has a genuine boolean type.
How are nested objects and arrays stored?
As minified JSON text in a single column. The column type is JSONB in PostgreSQL, JSON in MySQL, TEXT in SQLite and NVARCHAR(MAX) in SQL Server. The tool does not split nested structures into separate tables; if you want a relational model, convert the nested array on its own and add the foreign key yourself.
Can I insert all rows with a single INSERT?
The tool writes one statement per record, which makes a faulty row easy to find and keeps the output working the same way in all four dialects. You can merge the rows into a multi-row VALUES list by hand; for speed, running the statements inside one transaction is usually enough.
What happens to column names with spaces or reserved words?
Because names are always quoted, reserved words such as order and select and names containing spaces remain valid. The quote character itself is doubled, an empty key gets a numbered name such as column7, and names that differ only in case get a suffix such as _2.
Are very large integers altered?
No. The tool copies numbers as the JSON spells them, without converting them to floating point. Values that fit in 64 bits become BIGINT; larger ones go into a NUMERIC column in PostgreSQL, DECIMAL(65,0) in MySQL and DECIMAL(38,0) in SQL Server, and into a text column in SQLite.
Is my JSON sent to a server?
No. The statements are generated in your browser. Even so, check your organisation's rules before pasting real customer data into any web page.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.
Sources
- PostgreSQL Documentation: Numeric Types (integer, bigint, numeric, double precision)
- PostgreSQL Documentation: Character Types (varchar and text)
- PostgreSQL Documentation: Lexical Structure (identifiers and string constants)
- MySQL 8.4 Reference Manual: Data Types
- MySQL 8.4 Reference Manual: String Data Type Syntax (VARCHAR, TEXT, LONGTEXT)
- SQLite Documentation: Datatypes In SQLite (storage classes and type affinity)
- Microsoft Learn: Data types (Transact-SQL)
- Microsoft Learn: nchar and nvarchar (Transact-SQL)
- IETF RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format