Load JSON Files Examples
On this page
Note
For more information on the LOAD DATA command, refer to LOAD DATA.
Examples
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:
-
true
was converted to"1"
for columnsb
, but not for columnwhole
."1"
was further converted to theBOOL
value1
. -
The escapes
"\u00AE"
and"\u0022"
were converted to UTF-8 for columns
, but not for columnwhole
.Note that whole
would have become invalid JSON if we had translated"\u0022"
. -
The second row was discarded because it failed to match the
WHERE
clause. -
None of the paths in
subvalue_
could be found in the third row, somapping DEFAULT
literals like'{"subobject":"replaced"}'
were assigned instead. -
We assigned
a
to an intermediate variable so that we could extract an array element in theSET
clause. -
The
top-level
JSON values inexample2.
were not all JSON objects.json "hello"
is a validtop-level
JSON value.
Last modified: April 8, 2025