Skip to main content

Format Settings

SereneDB can attempt to determine the format of a JSON file when setting format to auto. Here are some example JSON files and the corresponding format settings that should be used.

In each of the below cases, the format setting was not needed, as SereneDB was able to infer it correctly, but it is included for illustrative purposes. A query of this shape would work in each case:

SQL
SELECT *FROM filename.json;
 key1   | key2
--------+--------
 value1 | value1
 value2 | value2
 value3 | value3

Format: newline_delimited​

With format = 'newline_delimited' newline-delimited JSON can be parsed. Each line is a JSON.

We use the example file records.json with the following content:

json
{"key1":"value1", "key2": "value1"}
{"key1":"value2", "key2": "value2"}
{"key1":"value3", "key2": "value3"}
SQL
SELECT *FROM read_json('records.json', format = 'newline_delimited');
 key1   | key2
--------+--------
 value1 | value1
 value2 | value2
 value3 | value3

Format: array​

If the JSON file contains a JSON array of objects (pretty-printed or not), array may be used. To demonstrate its use, we use the example file records-in-array.json:

json
[
{ "key1": "value1", "key2": "value1" },
{ "key1": "value2", "key2": "value2" },
{ "key1": "value3", "key2": "value3" }
]
SQL
SELECT *FROM read_json('records-in-array.json', format = 'array');
 key1   | key2
--------+--------
 value1 | value1
 value2 | value2
 value3 | value3

Format: unstructured​

If the JSON file contains JSON that is not newline-delimited or an array, unstructured may be used. To demonstrate its use, we use the example file unstructured.json:

json
{
"key1":"value1",
"key2":"value1"
}
{
"key1":"value2",
"key2":"value2"
}
{
"key1":"value3",
"key2":"value3"
}
SQL
SELECT *FROM read_json('unstructured.json', format = 'unstructured');
 key1   | key2
--------+--------
 value1 | value1
 value2 | value2
 value3 | value3

records Options​

SereneDB can attempt to determine whether a JSON file contains records when setting records = auto. When records = true, SereneDB expects JSON objects, and will unpack the fields of JSON objects into individual columns.

Continuing with the same example file, records.json:

json
{"key1":"value1", "key2": "value1"}
{"key1":"value2", "key2": "value2"}
{"key1":"value3", "key2": "value3"}
SQL
SELECT *FROM read_json('records.json', records = true);
 key1   | key2
--------+--------
 value1 | value1
 value2 | value2
 value3 | value3

When records = false, SereneDB will not unpack the top-level objects, and create STRUCTs instead:

SQL
SELECT *FROM read_json('records.json', records = false);
 json
-----------------
 (value1,value1)
 (value2,value2)
 (value3,value3)

This is especially useful if we have non-object JSON, for example, arrays.json:

json
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
SQL
SELECT *FROM read_json('arrays.json', records = false);
 json
---------
 {1,2,3}
 {4,5,6}
 {7,8,9}