Load JSON Files Examples
On this page
Note
For more information on the LOAD DATA command, refer to LOAD DATA.
To use an ENCLOSED BY <char> as a terminating field, a TERMINATED BY clause is needed.ENCLOSED BY <char> appearing within a field value can be duplicated, and they will be understood as a singular occurrence of the character.
If an ENCLOSED BY "" is used, quotes are treated as follows:
-
"The ""NEW"" employee" → The "NEW" employee
-
The "NEW" employee → The "NEW" employee
-
The ""NEW"" employee → The ""NEW"" employee
Example 1
If example. consists of:
{"a":{"b":1}, "c":null}{"a":{"b":2}, "d":null}
Then it can be loaded as follows:
CREATE TABLE t(a INT);LOAD DATA LOCAL INFILE "example.json" INTO TABLE t(a <- a::b) FORMAT JSON;SELECT * FROM t;
+------+
| a |
+------+
| 1 |
| 2 |
+------+Example 2
If example2. consists of:
{"b":true, "s":"A\u00AE\u0022A", "n":-1.4820790816978637e-25, "a":[1,2], "o":{"subobject":1}}{"b":false}"hello"
Then we can perform a more complicated LOAD DATA:
CREATE TABLE t(b bool NOT NULL, s TEXT, n DOUBLE, a INT, o JSON NOT NULL, whole longblob);LOAD DATA LOCAL INFILE "example2.json" INTO TABLE t FORMAT JSON(b <- b default true,s <- s default NULL,n <- n default NULL,@avar <- a default NULL,o <- o default '{"subobject":"replaced"}',whole <- %)SET a = json_extract_double(@avar, 1)WHERE b = true;SELECT * FROM t;
+---+-------+-------------------------+------+--------------------------+-----------------------------------------------------------------------------------------------+
| b | s | n | a | o | whole |
+---+-------+-------------------------+------+--------------------------+-----------------------------------------------------------------------------------------------+
| 1 | A®"A | -1.4820790816978637e-25 | 2 | {"subobject":1} | {"b":true, "s":"A\u00AE\u0022A", "n":-1.4820790816978637e-25, "a":[1,2], "o":{"subobject":1}} |
| 1 | NULL | NULL | NULL | {"subobject":"replaced"} | hello |
+---+-------+-------------------------+------+--------------------------+-----------------------------------------------------------------------------------------------+There are several things to note in the example above:
-
truewas converted to"1"for columnsb, but not for columnwhole."1"was further converted to theBOOLvalue1. -
The escapes
"\u00AE"and"\u0022"were converted to UTF-8 for columns, but not for columnwhole.Note that wholewould have become invalid JSON if we had translated"\u0022". -
The second row was discarded because it failed to match the
WHEREclause. -
None of the paths in
subvalue_could be found in the third row, somapping DEFAULTliterals like'{"subobject":"replaced"}'were assigned instead. -
We assigned
ato an intermediate variable so that we could extract an array element in theSETclause. -
The
top-level
JSON values inexample2.were not all JSON objects.json "hello"is a validtop-level
JSON value.
Last modified: September 11, 2025