Scalar Extraction from JSON
These examples load a scalar into theVARCHAR column named customer_name. Use the CREATE TABLE SQL statement to create the table with this column.
SQL
JSON
$name.
SQL
SELECT statement in JSON_FIELDS of the CREATE PIPELINE SQL statement.
For a JSON file that has a nested value, you need a complex selector.
JSON
CREATE PIPELINE statement for this file has $a.b.name instead of $name.
SQL
NULL and Empty Handling for JSON Scalars
The System handles all JSON NULL, empty, and missing values in the same way. The system loads these values asNULL. These values fail to load into non-nullable columns. Provide an explicit default in the pipeline or use the COLUMN_DEFAULT_IF_NULL option to accept the configured column default instead of attempting to load NULL values.
The JSON data contains this information.
JSON
SQL
SQL
JSON
JSON
JSON
Array Extraction from JSON
Supported array extraction scenarios are:- One-dimensional arrays.
- Multi-dimensional arrays.
- Array projection is the nested application of JSON selectors over the elements of arrays for both one-dimensional and multidimensional arrays.
- Arbitrary nesting of arrays within JSON objects, including splitting array dimensions across the JSON path.
- Loading individual array elements, which can also be arrays.
One-Dimensional Arrays
The next few examples load the same data into themy_table table both directly and using array projection.
SQL
JSON
SQL
SQL
Array Projection
Array projection is the application of the specified selector over all elements in an array, similar to a for loop. Load data using array projection into the same table using this JSON data.JSON
SQL
SQL
JSON
SQL
SQL
Multi-Dimensional Arrays
Multi-dimensional arrays work similarly to one-dimensional arrays. These examples load the same data into themy_table table both directly, and then using array projection.
SQL
JSON
SQL
JSON
$a[][][].b.name accesses each object, such as {"b": {"name": "John", "hometown": "Chicago"}}, and uses b.name to apply this nested attribute selector on all of the elements in the inner arrays.
SQL
SQL
Multi-Dimensional Arrays With Dimensions Split Across the JSON Path
To load a two-dimensional array, for example, use a one-dimensional array that contains JSON objects, each of which contains another one-dimensional array. Load the relevant parts of this JSON object into a two-dimensional array. In this example, the column hasVARCHAR[][] type and the SELECT expression is $x.a[].b.c[].name. The loaded data is a two-dimensional array of names.
The JSON file contains this information.
JSON
SQL
SQL
Extract Data from Individual Array Elements
Extract data from an element of a JSON array. That element can also be an array. In this example, extract the first element ofa, which is an array. JSON array indexes start at 1 so that all array indexing in Pipelines are consistent with the SQL standard.
The JSON file contains this information.
JSON
SQL
SQL
SQL
NULL and Empty Handling for JSON Arrays
The Ocient System handles NULL, empty, and missing values the same way for arrays as for scalars. The system converts a value that is NULL, empty, or missing to NULL and loads it asNULL.
Provide an explicit default in the pipeline or use the COLUMN_DEFAULT_IF_NULL option to accept the configured column default instead of attempting to load NULL.
Tuples of Scalars
Data pipeline loading supports tuples of scalars in the JSON source. The specification of tuple elements using curly braces is only supported for the basic case (i.e.$a.{b,c}). More complex selectors are not supported.
The recommended way of specifying tuples is to use an individual JSON selector for each tuple element, such as $a.b or $a.c.
You can apply functions to tuple elements.
Tuple Construction
This example loads two strings into a simple tuple.JSON
SQL
SQL
SQL
Advanced Tuple Construction
Use these examples to explore how to construct more complex tuples with nested objects and arrays. Select Fields from a Nested Object into a Tuple When the target fields are nested inside a named object, prefix the braces with the path to that object. Selector:Text
Text
TUPLE<<VARCHAR, INT>> from fields b and c inside object a.
SQL
Text
[] to the parent path maps the tuple selector over every element in the array, producing an array of tuples.
Selector:
Text
Text
TUPLE<<INT, VARCHAR>>[] from an array of objects, each containing fields b and c.
SQL
Text
[] to indicate that the field is an array. This selector produces a tuple with one element, an array, and the other a scalar.
Selector:
Text
Text
TUPLE<<VARCHAR[], INT>> where the first element is an array of strings and the second element is an integer.
SQL
Text
[] on the parent path with [] on a child field produces an array of tuples where each tuple contains an array element and a scalar element.
Selector:
Text
Text
TUPLE<<VARCHAR[], INT>>[] from an array of objects that each contain a nested array.
SQL
Text
Text
Text
TUPLE<<TUPLE<<VARCHAR, INT>>, TUPLE<<INT, VARCHAR>>>> from two nested objects.
SQL
Text
. (with no field name) creates an inline tuple element within an outer tuple. When you combine such an element with brackets [] on the parent path, this selector produces an array of tuples where one element is a tuple.
Selector:
Text
Text
TUPLE<<INT, TUPLE<<VARCHAR, VARCHAR, VARCHAR>>>>[] by selecting a scalar field and an inline tuple of indexed array elements.
SQL
Text
Applying Functions (Transformations) To Tuple Elements
In this example, load a tuple where elements have different types,VARCHAR and TIMESTAMP, and where the second element has to be converted from a JSON string into an Ocient timestamp. The JSON file contains this information.
JSON
SQL
SQL
SQL
NULL and Empty Handling for JSON Tuples
All the rules for handling NULL, empty, and missing elements that apply to scalars and arrays also apply to tuples. If any part of the selector is NULL, empty, or missing, data pipeline loading converts that value toNULL.
Additionally, because you can apply functions to tuple elements (and not array elements), you can use the NULL_IF function to convert a tuple element to NULL. For example, tuple<<char,varchar>>( $a.name, NULL_IF($a.hometown, 'N/A') ) indicates to the pipeline that the string 'N/A' signifies NULL for the hometown element but not for the name element.
Special Characters in JSON Keys
This example shows loading data where the JSON key names contain a special character. Create a table for the load using thecustomer_name column.
SQL
- special character.
JSON
$first-name in the CREATE PIPELINE SQL statement.
SQL
Array Extraction Operations
Other operations exist in the source field selector syntax to flatten or compact arrays while extracting the data.Flatten Arrays
Flattening means that the extracted array should not increase the rank, and instead, sub-arrays should be concatenated. To flatten an array, use the underscore character in the array selector (e.g.,$data.array[_]).
Example
This JSON data contains a nested array with two sub-arrays having data [1,2,3] and [4,5,6].
JSON
SQL
[1,2,3,4,5,6]
This selected array has rank 1, and not 2. Contrast this with the non-flattened selector a.b[].c[], which would have the output [[1,2,3],[4,5,6]].
You can also flatten arrays with the FLATTEN function. For details, see Array Data Transformation Functions. The functionality of the operator and function is equivalent.
Compact Arrays
Compaction eliminates NULL values from the output data. The exact bit pattern of NULL in the source data is source-type dependent. For JSON,null is a literal keyword that is unambiguous. For CSV or other less-defined types, the configuration determines which exact bits equate to NULL. To compact an array, use the exclamation mark character in the array selector(e.g., $data.array[!]).
Example
This JSON data contains a nested array with four NULL values.
JSON
SQL
[1,2,3]
You can also compact arrays with the ARRAY_COMPACT function. For details, see Array Data Transformation Functions. The functionality of the operator and function is equivalent.

