Skip to content
toolbelt
Back to Guides

Loading JSON Into a Database Without Quote Bugs

By ·

Updated

The name is O’Brien and your hand-built INSERT breaks. Here is how escaping actually works.

What is this tool?

Generating INSERT statements from JSON means turning objects into rows. The interesting part is not the syntax, it is the quoting.

How it works

Every object contributes its keys to one column list, so rows with different shapes still produce valid SQL. Values are converted by type and strings have their apostrophes doubled. Identifiers are double quoted.

Example

[{"id":1,"name":"O'Brien","active":true}]

becomes

INSERT INTO "users" ("id", "name", "active")
VALUES
  (1, 'O''Brien', TRUE);

When to use it

Seeding a development database. Moving data between systems that both speak JSON. Building a reproduction case that needs specific rows.

Optimization Tips

Keep batches in the low hundreds. Very large multi-row inserts hit server statement limits, and failing halfway is worse than several smaller runs. Quote identifiers if a column is a reserved word like order or group.

Frequently Asked Questions

Which databases accept this?
PostgreSQL, SQLite, and MySQL with ANSI_QUOTES. MySQL default mode uses backticks.
How are nulls and booleans handled?
null becomes NULL, true becomes TRUE. MySQL stores that as 1 in a tinyint.
Is this safe?
For fixtures, yes. For application data at runtime, use parameterised queries instead of building SQL text.
Rows with different keys?
The column list is the union, and missing values become NULL.

Ready to try it out?

Put this knowledge into practice with our free tool.

Go to Tool

Related tools