Load JSON Files Examples

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, and WHERE clauses 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_values.json 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_mixed.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 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;

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 true to "1" for column b, but preserves the original JSON value in column whole. It then converts "1" to the Boolean value 1 for column b.

  • SingleStore converts the escape sequences \u00AE and \u0022 to UTF-8 characters for column s, but preserves the original JSON value in column whole. Converting \u0022 in the column whole would make the JSON document invalid.

  • The WHERE clause discards the second row because it does not satisfy the specified condition.

  • SingleStore assigns DEFAULT values, such as '{"subobject":"replaced"}', when it cannot find the paths specified in subvalue_mapping in the third row.

  • The example assigns a to an intermediate variable so that the SET clause can extract an element from the array.

  • The JSON file json_mixed.json contains top-level JSON values that are not JSON objects. For example, "hello" is a valid top-level JSON value.

Last modified:

Was this article helpful?

Verification instructions

Note: You must install cosign to verify the authenticity of the SingleStore file.

Use the following steps to verify the authenticity of singlestoredb-server, singlestoredb-toolbox, singlestoredb-studio, and singlestore-client SingleStore files that have been downloaded.

You may perform the following steps on any computer that can run cosign, such as the main deployment host of the cluster.

  1. (Optional) Run the following command to view the associated signature files.

    curl undefined
  2. Download the signature file from the SingleStore release server.

    • Option 1: Click the Download Signature button next to the SingleStore file.

    • Option 2: Copy and paste the following URL into the address bar of your browser and save the signature file.

    • Option 3: Run the following command to download the signature file.

      curl -O undefined
  3. After the signature file has been downloaded, run the following command to verify the authenticity of the SingleStore file.

    echo -n undefined |
    cosign verify-blob --certificate-oidc-issuer https://oidc.eks.us-east-1.amazonaws.com/id/CCDCDBA1379A5596AB5B2E46DCA385BC \
    --certificate-identity https://kubernetes.io/namespaces/freya-production/serviceaccounts/job-worker \
    --bundle undefined \
    --new-bundle-format -
    Verified OK

Try Out This Notebook to See What’s Possible in SingleStore

Get access to other groundbreaking datasets and engage with our community for expert advice.