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:
-
Map JSON fields to multiple table columns.
-
Load entire JSON values into a single JSON column.
-
Use
DEFAULT,SET, andWHEREclauses when loading JSON data. -
Load a JSON file that contains a top-level array of objects using a pipeline.
-
Extract nested JSON fields using the
:: subvalue_syntax in a pipeline.path
Refer to Load JSON Files with LOAD DATA for more information on syntax and usage.
Map JSON Fields to Multiple Columns
This example extracts values from JSON fields and loads them into individual table columns.
This example uses books. file which contains the following JSON data:
{"title": "Onyx Storm","author": "Rebecca Yarros","details": {"publisher": "Entangled:Red Tower Books","numpages": 544,"publication date": "January 21, 2025","printings": [2,1.3],"series": "The Empyrean"}}{"title": "The Maid","author": "Nita Prose","details": {"publisher": "Ballantine Books","numpages": 385,"publication date": "January 4, 2022","printings": [0.5,0.75,1.2]}}{"title": "The Last Letter","author": "Rebecca Yarros","details": {"publisher": "Entangled:Amara","numpages": 432,"publication date": "February 26, 2019","printings": [0.25,0.5,0.5]}}
Create the table named books_.
CREATE TABLE books_json_multi_col(title_s2 TEXT,author_s2 TEXT,publisher_s2 TEXT,numpages_s2 INT,publication_date_s2 DATE,printings_s2 TEXT,series_s2 TEXT);
Load the JSON Data into the table books_.
LOAD DATA S3 's3://singlestore-docs-example-datasets/json/books.json'INTO TABLE books_json_multi_col(title_s2 <- title,author_s2 <- author,publisher_s2 <- details::publisher,numpages_s2 <- details::numpages,@date_variable <- details::`publication date`,printings_s2 <- details::printings,series_s2 <- details::series DEFAULT NULL)FORMAT JSONSET publication_date_s2 = TO_DATE(@date_variable, 'MONTH DD, YYYY');
In this example:
-
Backticks (
`) are used around the JSON field name with a space (`publication date`). -
A variable (
@date_) and avariable SETstatement are used to convert the date string in the JSON file to a SingleStoreDATEtype. -
A default value is provided for the
series_column as not all JSON values have a series field.s2
Verify the results:
SELECT * FROM books_json_multi_col;
+-----------------+----------------+---------------------------+-------------+---------------------+----------------+--------------+
| title_s2 | author_s2 | publisher_s2 | numpages_s2 | publication_date_s2 | printings_s2 | series_s2 |
+-----------------+----------------+---------------------------+-------------+---------------------+----------------+--------------+
| Onyx Storm | Rebecca Yarros | Entangled:Red Tower Books | 544 | 2025-01-21 | [2,1.3] | The Empyrean |
| The Maid | Nita Prose | Ballantine Books | 385 | 2022-01-04 | [0.5,0.75,1.2] | NULL |
| The Last Letter | Rebecca Yarros | Entangled:Amara | 432 | 2019-02-26 | [0.25,0.5,0.5] | NULL |
+-----------------+----------------+---------------------------+-------------+---------------------+----------------+--------------+Load Entire JSON Values into a Single Column
This example uses books. and loads each JSON value into a single column of type JSON.books. file.
Create the table named books_.
CREATE TABLE books_json_single_col(json JSON);
Load the JSON Data in the table books_.
LOAD DATA S3 's3://singlestore-docs-example-datasets/json/books.json'INTO TABLE books_json_single_col(json <- %)FORMAT JSON;
The % subvalue mapping loads the complete JSON value into the target column.
Verify the results:
SELECT JSON_PRETTY(json) FROM books_json_single_col;
Note
JSON_ formats JSON values for readability.
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.
Load a JSON File with a Top-Level Array
This example loads the following JSON file that contains a top-level array of objects using a pipeline.
This example uses jsonarrays. file which contains the following JSON data:
[{"title": "Onyx Storm","author": "Rebecca Yarros","numpages": 544},{"title": "The Maid","author": "Nita Prose","numpages": 385},{"title": "The Last Letter","author": "Rebecca Yarros","numpages": 432}]
Create a table named books_.
CREATE TABLE books_s2 (title_s2 TEXT,author_s2 TEXT,numpages_s2 INT);
Create a pipeline named books_.
CREATE PIPELINE books_pipe ASLOAD DATA S3 's3://singlestore-docs-example-datasets/json/jsonarrays.json'INTO TABLE books_s2 (title_s2 <- title,author_s2 <- author,numpages_s2 <- numpages)FORMAT JSON;
Start the pipeline.
START PIPELINE books_pipe FOREGROUND;
Verify the results:
SELECT * FROM books_s2;
+-----------------+----------------+-------------+
| title_s2 | author_s2 | numpages_s2 |
+-----------------+----------------+-------------+
| Onyx Storm | Rebecca Yarros | 544 |
| The Maid | Nita Prose | 385 |
| The Last Letter | Rebecca Yarros | 432 |
+-----------------+----------------+-------------+In this example, SingleStore processes JSON files that contain a top-level array.
-
The pipeline loads each object in the array as a separate row.
-
SingleStore pipelines support top-level JSON arrays for both
INTO TABLEandINTO PROCEDUREtargets. -
You can also load the same JSON file by using
LOAD DATA … FORMAT JSON.
Extract Nested JSON Fields Using a Pipeline
This example uses books. and creates a pipeline that extracts values from nested JSON fields using the :: subvalue path syntax.
Create a table named books_.
CREATE TABLE books_nested (title_s2 TEXT,author_s2 TEXT,publisher_s2 TEXT,numpages_s2 INT,printings_s2 JSON,series_s2 TEXT);
Create a pipeline named books_.
CREATE PIPELINE books_nested_pipe ASLOAD DATA S3 's3://singlestore-docs-example-datasets/json/books.json'INTO TABLE books_nested (title_s2 <- title,author_s2 <- author,publisher_s2 <- details::publisher,numpages_s2 <- details::numpages,printings_s2 <- details::printings,series_s2 <- details::series DEFAULT NULL)FORMAT JSON;
Start the pipeline:
START PIPELINE books_nested_pipe FOREGROUND;
Verify the results:
SELECT * FROM books_nested;
+-----------------+----------------+---------------------------+-------------+----------------+--------------+
| title_s2 | author_s2 | publisher_s2 | numpages_s2 | printings_s2 | series_s2 |
+-----------------+----------------+---------------------------+-------------+----------------+--------------+
| Onyx Storm | Rebecca Yarros | Entangled:Red Tower Books | 544 | [2,1.3] | The Empyrean |
| The Maid | Nita Prose | Ballantine Books | 385 | [0.5,0.75,1.2] | NULL |
| The Last Letter | Rebecca Yarros | Entangled:Amara | 432 | [0.25,0.5,0.5] | NULL |
+-----------------+----------------+---------------------------+-------------+----------------+--------------+In this example:
-
The
details::publisher,details::numpages,details::printings, anddetails::seriessubvalue paths extract values from the nested details object. -
The
::separator navigates nested JSON objects.For deeper nesting, chain additional identifiers (for example, level1::level2::level3). -
SingleStore assigns a
DEFAULT NULLvalue toseries_because not all JSON values contain a series field in the details object.s2
Last modified: