Load JSON Files Examples
On this page
This topic demonstrates common methods for loading JSON data into SingleStore using the LOAD DATA command.
The examples show how to:
-
Load a nested JSON value into a single column.
-
Use
DEFAULT,SET, andWHEREclauses when loading JSON data.
Refer to Load JSON Files with LOAD DATA for more information on syntax and usage.
Load Nested JSON Values
This example loads nested JSON values into a single table column using the :: subvalue path syntax.
This example uses nested_ file which contains the following JSON data:
{"a":{"b":1}, "c":null}{"a":{"b":2}, "d":null}
Create a table named t.
CREATE TABLE t(a INT);
Load the nested JSON value into column a of the table t.
LOAD DATA LOCAL INFILE "nested_values.json"INTO TABLE t(a <- a::b)FORMAT JSON;
Verify the results:
SELECT * FROM t;
+------+
| a |
+------+
| 1 |
| 2 |
+------+Use DEFAULT, SET, and WHERE Clauses
This example uses DEFAULT, SET, and WHERE clauses to assign fallback values, transform extracted data, and filter rows when loading JSON data.
This example uses json_ file which contains the following JSON data:
{"b":true, "s":"A\u00AE\u0022A", "n":-1.4820790816978637e-25, "a":[1,2], "o":{"subobject":1}}{"b":false}"hello"
Create the table named t.
CREATE TABLE t(b BOOL NOT NULL,s TEXT,n DOUBLE,a INT,o JSON NOT NULL,whole LONGBLOB);
Load the JSON data into the table t.
LOAD DATA LOCAL INFILE 'json_mixed.json'INTO TABLE tFORMAT 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;
Verify the results:
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 |
+---+-------+-------------------------+------+--------------------------+-----------------------------------------------------------------------------------------------+In this example, there are several JSON loading behaviors:
-
SingleStore converts
trueto"1"for columnb, but preserves the original JSON value in columnwhole.It then converts "1"to the Boolean value1for columnb. -
SingleStore converts the escape sequences
\u00AEand\u0022to UTF-8 characters for columns, but preserves the original JSON value in columnwhole.Converting \u0022in the columnwholewould make the JSON document invalid. -
The
WHEREclause discards the second row because it does not satisfy the specified condition. -
SingleStore assigns
DEFAULTvalues, such as'{"subobject":"replaced"}', when it cannot find the paths specified insubvalue_in the third row.mapping -
The example assigns
ato an intermediate variable so that theSETclause can extract an element from the array. -
The JSON file
json_contains top-level JSON values that are not JSON objects.mixed. json For example, "hello"is a valid top-level JSON value.
Last modified: