Purpose
JSON_TABLE creates a temporary table based on all, or a subset, of the data in a JSON object.
Syntax
Syntax Elements
- json_documents_retrieving_expr
- A query expression that must result in at least two columns. The first column is an ID that identifies a JSON document. The ID is returned in the output row to correlate output rows to the input rows, and in case of errors, it is used to identify the input row that caused the error. The second column is the JSON document itself. Other columns can be included, but they must be after the JSON document column. Extra columns are returned in the output row without being modified from the input.
- row_expr_literal
- A LATIN or UNICODE string literal that conforms to JSONPath syntax.
- column_expr_literal
- A LATIN or UNICODE literal that represents a JSON array. This array contains one or more JSON objects, each of which defines a column to be produced by the JSON_TABLE operator.
- AS
- Optional keyword introducing correlation_name.
- correlation_name
- An alias for the table that is referenced by json_documents_retrieving_expr.
- column_name
- An optional list of one or more column names.
Functional Description
JSON_TABLE takes a JSON instance and creates a temporary table based on all, or a subset, of the data in the instance. The JSON instance is retrieved from a table with an SQL statement. JSONPath syntax is used to define the portions of the JSON instance to retrieve.
Return Value
The JSON_TABLE table operator produces output rows which conform to the row format defined by the column_expr_literal. That literal describes the columns in the output row and their data types.
- The first column returned contains the JSON document ID obtained from the first column in the json_documents_retrieving_expression.
- The next N columns returned are generated based on the colexpr parameter, where N is the number of objects in the JSON array represented by the column_expression_literal.
- If json_documents_retrieving_expr returns more than two columns, all the extra columns from the third column onward are added to the output row without being modified.
- Ordinal Column
- An Ordinal Column contains an integer sequence number. The sequence number is not guaranteed to be unique in itself, but the combination of the id column, the first column of the output row, and the ordinal column is unique.
- Normal Column Relative to Row
- The value of this column is determined by executing a JSONPath query. The query is evaluated with an object returned by the ROWEXPR JSONPath query as the root object.
- Normal Column Relative to Root
- The value of this column is determined by executing a JSONPath query. The query is evaluated with the object returned by json_documents_retrieving_expression, the second column, as the root object. This type of column is identified by setting "fromRoot" to true.
JSON_TABLE does not support UDTs and LOB types in the output so the JSON data type cannot be the type for the output columns. The data type of the columns of the output table may be any non-LOB predefined Teradata type. The supported data types for JSON_TABLE output are listed next.
Supported Data Types
CHAR(n) |
VARCHAR(n) |
BYTE(n) |
VARBYTE(n) |
BYTEINT |
SMALLINT |
INTEGER |
FLOAT/REAL |
DECIMAL/NUMERIC [(n,[m])] |
NUMBER |
DATE |
TIME [(fractional_seconds_precision)] |
TIME [(fractional_seconds_precision)] WITH TIME ZONE |
TIMESTAMP [(fractional_seconds_precision)] |
TIMESTAMP [(fractional_seconds_precision)] WITH TIME ZONE |
INTERVAL YEAR [(precision)] |
INTERVAL YEAR [(precision)] TO MONTH |
INTERVAL MONTH [(precision)] |
INTERVAL DAY[(precision)] |
INTERVAL DAY[(precision)] TO HOUR |
INTERVAL DAY[(precision)] TO MINUTE |
INTERVAL DAY [(precision)] TO SECOND |
INTERVAL HOUR [(precision)] |
INTERVAL HOUR [(precision)] TO MINUTE |
INTERVAL HOUR [(precision)] TO SECOND |
INTERVAL MINUTE [(precision)] |
INTERVAL MINUTE [(precision)] TO SECOND |
INTERVAL SECOND [(fractional_seconds_precision)] |
Rules and Restrictions
This function resides in TD_SYSFNLIB. The ID column of the query result can be any number type or character type excluding CLOB. It is the responsibility of the user to make the ID column unique. If it is not unique JSON_TABLE does not fail, but it is difficult to tell which rows came from which JSON document in the resulting output.
The result is subject to the maximum row size, and the query must not exceed the maximum allowable size for a query.