The json extension adds support for the JSON datatype, including a set of functions for JSON
access and manipulation, scanning from, and copying to JSON files.
You can install the JSON extension by running the following command:
This feature allows you to scan JSON files, similar to scanning other formats.
Without type information, the structure will be inferred.
To declare type information, you can use LOAD WITH HEADERS like you would for CSV files.
To scan the file above, you can do the following:
Output:
Note that the resulting schema will be the union of the schema of all the objects in the json file.
In the above case, because we loosely scanned the file with no enforcement of types, column registry_date
will be interpreted as a STRING, and not DATE by default. To enforce the datatype during scanning,
use the LOAD WITH HEADERS feature.
Example:
Output:
The following optional parameters are supported:
Name
Description
format
"array" or "unstructured". Array will read from documents whose roots are arrays, unstructured will read from files with multiple documents. Default value is "array".
maximum_depth
Default value is 10. Used by the type inference system to determine how “deep” into the json document to go to infer types.
sample_size
Default value 2048. Used by the type inference system to determine the number of elements used to infer the json type.
format Parameter
Given a ‘people-unstructured.json’ file in the following form:
That is, the file contains multiple JSON objects instead of objects wrapped in an array, then the
format='unstructured' option should be used to scan the file.
The results remain the same.
maximum_depth and sample_size Parameters
If the types aren’t explicitly declared, the type inference system will only go through the first
sample_size objects, and only reach a depth of maximum_depth to infer the schema. If we write
Then the type inference system will only look at
And conclude that the schema is (ID UINT8, NAME STRING), therefore giving the result
Likewise, if we set the maximum_depth to 2
Then the type inference system will give up after the second level of type inference,
and assume everything is a STRING
COPY FROM
This feature allows you to copy data from a JSON file into a node or relationship table.
Example:
Optional parameters in COPY FROM are identical to the LOAD FROM feature, shown above.
COPY TO
This feature allows you to copy data from an existing table in Kùzu to a JSON file.
Example usage:
The output in the file people.json-output looks like the following: