JSON_Data_Type
JSON_Data_Type
Data Type
Release 17.00
September 2020
B035-1150-170K
DOCS.TERADATA.COM
Copyright and Trademarks
Copyright © 2014 - 2020 by Teradata. All Rights Reserved.
All copyrights and trademarks used in Teradata documentation are the property of their respective owners. For more information, see
Trademark Information.
Product Safety
Indicates a situation which, if not avoided, could result in damage to property, such as to equipment
NOTICE or data, but not related to personal injury.
Indicates a hazardous situation which, if not avoided, could result in minor or moderate
CAUTION personal injury.
Indicates a hazardous situation which, if not avoided, could result in death or serious
WARNING personal injury.
Third-Party Materials
Non-Teradata (i.e., third-party) sites, documents or communications (“Third-party Materials”) may be accessed or accessible (e.g., linked or
posted) in or in connection with a Teradata site, document or communication. Such Third-party Materials are provided for your convenience
only and do not imply any endorsement of any third party by Teradata or any endorsement of Teradata by such third party. Teradata is not
responsible for the accuracy of any content contained within such Third-party Materials, which are provided on an “AS IS” basis by Teradata.
Such third party is solely and directly responsible for its sites, documents and communications and any harm they may cause you or others.
Warranty Disclaimer
Except as may be provided in a separate written agreement with Teradata or required by applicable law, the information available
from the Teradata Documentation website or contained in Teradata information products is provided on an "as-is" basis, without
warranty of any kind, either express or implied, including the implied warranties of merchantability, fitness for a particular
purpose, or noninfringement.
The information available from the Teradata Documentation website or contained in Teradata information products may contain references
or cross-references to features, functions, products, or services that are not announced or available in your country. Such references do not
imply that Teradata Corporation intends to announce such features, functions, products, or services in your country. Please consult your
local Teradata Corporation representative for those features, functions, products, or services available in your country.
The information available from the Teradata Documentation website or contained in Teradata information products may be changed or
updated by Teradata at any time without notice. Teradata may also make changes in the products or services described in this information
at any time without notice.
Machine-Assisted Translation
Certain materials on this website have been translated using machine-assisted translation software/tools. Machine-assisted translations
of any materials into languages other than English are intended solely as a convenience to the non-English-reading users and are not
legally binding. Anybody relying on such information does so at his or her own risk. No automated translation is perfect nor is it intended to
replace human translators. Teradata does not make any promises, assurances, or guarantees as to the accuracy of the machine-assisted
translations provided. Teradata accepts no responsibility and shall not be liable for any damage or issues that may result from using such
translations. Users are reminded to use the English contents.
Feedback
To maintain the quality of our products and services, e-mail your comments on the accuracy, clarity, organization, and value of this document
to: docs@teradata.com.
Any comments or materials (collectively referred to as "Feedback") sent to Teradata Corporation will be deemed nonconfidential. Without
any payment or other obligation of any kind and without any restriction of any kind, Teradata and its affiliates are hereby free to (1) reproduce,
distribute, provide access to, publish, transmit, publicly display, publicly perform, and create derivative works of, the Feedback, (2) use any
ideas, concepts, know-how, and techniques contained in such Feedback for any purpose whatsoever, including developing, manufacturing,
and marketing products and services incorporating the Feedback, and (3) authorize others to do any or all of the above.
Contents
The JSON data type is provided by Vantage. You can use it for JSON format data, similarly to how you use
other SQL data types.
• JSON data type content is stored in an optimized format depending on the size of the data.
• The user is not responsible for executing the CREATE TYPE statement for the JSON data type. JSON
data types do not need to be created via DDL by the user as the JSON data type exists in the database.
• The JSON data type cannot be created, dropped, or altered by the user.
• There are two points of commonality between all uses of the JSON data type in any valid location:
◦ The maximum length of any instance of the JSON data type is variable. You may set the maximum
length of the JSON instance, otherwise the default maximum length is used.
◦ The CHARACTER SET for the text of the JSON data type can be either UNICODE or LATIN. If you
do not specify the character set, the default character set of the user is used.
• JSONPath provides simple traversal and regular expressions with wildcards to filter and navigate
complex JSON documents.
Client
Product JSON Support Provided
ODBC The ODBC specification does not have a unique data type code for JSON. Therefore,
the ODBC driver maps the JSON data type to SQL_LONGVARCHAR or SQL_
WLONGVARCHAR, which are the ODBC CLOB data types. The metadata clearly
differentiates between a Vantage CLOB data type mapped to SQL_LONGVARCHAR
and a Vantage JSON data type mapped to SQL_LONGVARCHAR.
The ODBC driver supports LOB Input, Output and InputOutput parameters. Therefore, it
can load JSON data. Also the Catalog (Data Dictionary) functions support JSON.
JDBC • Teradata JDBC Driver 15.00.00.11 and later support the JSON data type.
• The Teradata JDBC Driver offers Vantage-specific functionality for an application to use
the PreparedStatement or CallableStatement setObject method to bind a Struct value to
a question-mark parameter marker as a JSON data type. An application can also insert
VARCHAR or CLOB values into JSON destination columns.
• When an application uses the Vantage-specific functionality of specifying a JSON value
as a Struct value, the Struct value must contain one of the following attributes: String,
Reader, Clob, or null. If the Struct contains a Reader attribute, the Struct must also
contain a second attribute that is an Integer type specifying the number of characters in
the stream.
• JSON values are retrieved from Vantage as CLOB values. An application can use result
set metadata or parameter metadata to distinguish a CLOB value from a JSON value.
.NET Data • JSON data type is externalized as a CLOB. Applications can use TdClob, TdDataReader.
Provider GetChars, or TdDataReader.GetString to retrieve a JSON value.
• Applications can send a JSON value as String or TextReader to the database.
• Schema Collections (Data Dictionary) also support the JSON data type.
Teradata JSON columns are treated exactly like CLOB columns and subject to the same limitations.
Parallel JSON columns cannot exceed 16 MB (16,776,192 LATIN characters or 8,388,096
Transporter UNICODE characters). Teradata PT accommodates the JSON keyword in object schema
(Teradata PT) but internally converts it to CLOB. Import and export are both fully supported.
BTEQ The JSON keyword cannot be used in the USING data statement; therefore, JSON values
must be referred to as either CLOB or VARCHAR. For VARCHAR, the JSON value cannot
exceed 64 KB (64000 LATIN characters or 32000 UNICODE characters).
Currently, BTEQ does not support deferred mode LOB transfer for server to client. Only
non-deferred mode JSON transfer is supported for server to client, and the maximum size
of an output row is limited to 64 KB.
Standalone No support.
Utilities
For more information about the Teradata client products, see the following documents:
Terminology
• A JSON document is any string that conforms to the JSON format. See JSON String Syntax.
• A JSON document can be structured as an object or as an array.
• A JSON object consists of zero or more name:value pairs delimited by curly braces ( { } ).
• The value portion of a JSON object can be a single string, number, Boolean (true or false), null, array,
or object.
• A JSON array is an ordered list of zero or more values delimited by square brackets ( [ ] ). Those
values can be a single string, number, Boolean true or false, null, array, or object.
• JSON documents that are stored and used as Vantage JSON data types can be referred to as JSON
instances or JSON type instances.
The following is an example of a JSON document.
{
"name": "Product",
"properties": {
"id": {
"type": "number",
"description": "Product identifier",
"required": true
},
"name": {
"type": "string",
"description": "Name of the product",
"required": true
},
"price": {
"type": "number",
"minimum": 0,
"required": true
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
},
"stock": {
"type": "object",
"properties": {
"warehouse": {
"type": "number"
},
"retail": {
"type": "number"
}
}
}
}
}
Standards Compliance
The Vantage JSON data type is compliant with the standard for JSON syntax defined by IETF RFC 4627.
The standard is available at http://www.ietf.org/rfc/rfc4627.txt and http://www.json.org/.
The Vantage implementation of the BSON storage format for the JSON type is compliant with the BSON
specification located at http://bsonspec.org/.
The Vantage implementation of the UBJSON storage format deviates slightly from the UBJSON
specification located at http://ubjson.org/. The proposed UBJSON standard specifies that numeric types are
stored in big-endian format. However, Vantage stores the data in little-endian format to optimize insertion
and retrieval times.
Syntax Elements
maxlength
A positive integer value followed by an optional multiplier. maxlength specifies the maximum
length of the JSON type as follows:
• If the storage format of the JSON type is text, the maximum length is in characters. If
you do not specify a maximum length, the default maximum length for the character
set is used. If specified, the length is subject to a minimum of two characters and
cannot be greater than the maximum of 16776192 LATIN characters or 8388096
UNICODE characters.
• If the storage format is BSON or UBJSON, the maximum length is in bytes. If you do not
specify a maximum length, the default maximum length of 16776192 bytes is used.
• The multiplier, if specified, is KkMm.
The length specified only covers the actual data length. The actual storage sizes include
additional header information.
CHARACTER SET
The character set for the JSON type can be LATIN or UNICODE.
If you do not specify a character set, the default character set for the user is used.
You cannot specify CHARACTER SET together with the STORAGE FORMAT clause.
STORAGE FORMAT
Specifies that the storage format of the JSON type will be one of the following binary formats:
• BSON (Binary JSON)
• UBJSON (Universal Binary JSON)
If you do not specify a storage format, the default storage format is text in the character set
specified (or the default character set).
You cannot specify STORAGE FORMAT together with the CHARACTER SET clause.
attribute
The following data type attributes are supported for the JSON type:
2. Use a query such as the following to determine the length needed for a JSON column which uses a
binary format:
This is one way to determine the smallest possible size for a JSON column. If space is not an issue and there
is no need to receive the performance benefits of a smaller JSON, then Teradata recommends to use the
maximum possible size for the JSON column.
*** JSON value is too large to store in the defined JSON type.
For example, if you try to insert data that is larger than the maximum length defined for a JSON column, or
pass data that is larger than what is supported by a JSON parameter, you will get this error.
For a JSON column that uses a binary format, you can load binary data that fits within the defined length of
the column, but this data may exceed the maximum allowed size for the output (text) format. In this case, the
data may be stored in a binary format, but it cannot be retrieved as text format. You will get this error when
trying to access this data as text. If you encounter this problem, you can do one of the following:
• Cast the data to a JSON type with a larger maximum length.
• Cast the data to a predefined type (VARCHAR/VARBYTE/CLOB/ BLOB).
• Select the data using the AsJSONText or AsBSON methods.
1. If the maximum length specified is less than or equal to 64000 bytes (64000 LATIN characters or 32000
UNICODE characters), then the data in all rows of the table are stored inline.
2. If the maximum length specified exceeds 64000 bytes, then rows with less than 4K bytes of data are
stored inline, and rows with more than 4K bytes of data are stored in a LOB subtable. A table with a
column defined in this manner will have a few rows stored inline and a few rows in a LOB subtable,
depending on data size.
Related Information:
Casting JSON Data
AsBSON
AsJSONText
StorageSize
Result: This creates a table with LATIN character set with a maximum length of that character set,
16776192 LATIN characters. When a character set is not specified for the JSON type, the default character
set for the user is used. The result for this example assumes the user had LATIN as their default
character set.
Example: Create a table with a JSON type column, with no maximum length specified and specify
UNICODE character set:
Result: This creates a table with UNICODE character set and a maximum length of that character set,
8388096 UNICODE characters.
Example: Create a table with a JSON type column, with a maximum length specified and no character
set specified:
Result: This creates a table with a maximum length of 100000 LATIN characters. Note, the result for this
example assumes the user had LATIN as their default character set.
Example: Create a table with a JSON type column, with a maximum length specified and UNICODE
character set specified:
Result: This creates a table with a maximum length of 100000 UNICODE characters.
Example: Create a table with JSON type columns, with a maximum length specified that exceeds the
allowed length and no character set specified:
Result: This fails because the maximum possible amount of data stored in the row could grow to
approximately 76000 bytes. This exceeds the maximum row size, as described in item 1 earlier.
Example: Create a table with JSON type columns, with a maximum length specified and no character
set specified:
Result: This succeeds because the maximum possible amount of data stored in the row is ~16000 bytes
which is within the maximum row size. This is because the json_j1 column has the storage scheme
described in item 2 earlier, in which a maximum of 4K bytes will be stored in the row.
LANGUAGE C
EXTERNAL NAME 'CS!json1!json1.c!F!json1';
void json1 (
JSON_HANDLE *json_handle,
VARCHAR_LATIN *result,
int *indicator_ary,
int *indicator_result,
char sqlstate[6],
SQL_TEXT extname[129],
SQL_TEXT specific_name[129],
SQL_TEXT error_message[257])
{
/* body function */
}
void json2 (
VARCHAR_LATIN *result,
JSON_HANDLE *json_handle,
int *indicator_ary,
int *indicator_result,
char sqlstate[6],
SQL_TEXT extname[129],
SQL_TEXT specific_name[129],
SQL_TEXT error_message[257])
{
/* body function */
}
Example: Error: JSON Value Is Too Large to Store in the Defined JSON Type
In this example, an error is returned when data being inserted into a JSON column is larger than the
maximum length defined.
The smallJSONTable table in this example has a JSON column with a maximum length of 10
LATIN characters.
The following INSERT statement succeeds because the data inserted into the JSON column is less than
10 characters.
The following INSERT statement fails because '{"a":12345}' is greater than the maximum length of
10 characters.
*** Failure 7548 JSON value is too large to store in the defined
JSON type.
Result:
*** Failure 7548 JSON value is too large to store in the defined JSON type.
In this case, the data may be cast to a predefined type (VARCHAR/VARBYTE/CLOB/BLOB), selected out
using the AsJSONText or AsBSON methods, or cast to a larger version of the JSON type in UNICODE
text format.
INSERT ubjsonTable(1,'[100,100,100,100,100,100,100,100,100,100,100,100]');
The following query uses the StorageSize method to get the size in bytes needed to store the above data
in UNICODE text.
Result:
ubjsonCol.STORAGESIZE('UNICODE_TEXT')
-------------------------------------
98
The following query uses the StorageSize method to get the size in bytes currently used to store the above
data in UBJSON format.
Result:
ubjsonCol.STORAGESIZE('UBJSON')
-------------------------------
40
The inserted data in UBJSON format is 40 bytes, and this fits into the ubjsonCol column which is defined
with a maximum length of 50 bytes.
The following query fails because it tries to retrieve the inserted data as UNICODE text. The inserted data
is 98 bytes in UNICODE text as shown in the previous query. This exceeds the maximum length of the
ubjsonCol column which is 50 bytes.
Result:
*** Failure 7548 JSON value is too large to store in the defined JSON type.
In order to retrieve the inserted data in UNICODE text format, you must cast the data to a larger size as
shown in the following query:
Result:
ubjsonCol
-------------------------------------------------
[100,100,100,100,100,100,100,100,100,100,100,100]
Note that the inline length cannot be larger than the maximum length specified for the JSON type.
Maximum
Data Type Inline Storage Length LOB Type
Maximum
Data Type Inline Storage Length LOB Type
The following is exactly the same as the example for jsonTable2, but with a different syntax.
• When storage compactness is desired and documents have a large amount of numbers as values
• When retrieval of portions of the document is more important or is done more frequently than insertion
of the data
Numeric types are always serialized in little-endian format. This is a deviation from the proposed UBJSON
standard which specifies that numeric types are stored in big-endian format. Vantage stores the data in
little-endian format to optimize insertion and retrieval times.
Data cannot be imported to or exported from the database in the UBJSON format. UBJSON encodes
strings in the UTF-8 character set, and the character set for UBJSON data when it is imported or exported
as text is UNICODE.
If you want to migrate the JSON data from being stored as text to being stored as BSON, you must
determine the maximum size needed using a query like the following:
The result of this query can then be used to create the new table. In this example, suppose the result was X.
Related Information:
Maximum Length of a JSON Instance
StorageSize
Syntax
Syntax Elements
JSON_string_spec
JSON_binary_data_spec
'JSON_String'
A text string that will be the value of the resulting JSON instance.
The string must conform to JSON syntax as described in JSON String Syntax.
LATIN
UNICODE
The character set of the resulting JSON instance.
If you do not specify a character set, the default character set of the user is used.
BSON
UBJSON
The storage format of the resulting JSON instance.
The result of the constructor is a JSON type with its data stored in either BSON or
UBJSON format.
JSON_binary_data
JSON binary data that will be the value of the resulting JSON instance.
The binary data must be BYTE, VARBYTE, or BLOB type.
If you specify JSON_binary_data, you must specify either BSON or UBJSON.
Usage Notes
In the default constructor, no arguments are passed to the constructor expression. NEW JSON() initializes
an empty JSON type value with the character set based on the character set of the user. The data is set to
a null string, and the default storage format is text.
If you pass an empty JSON object as an argument to the constructor, for example, NEW JSON(''), the
constructor returns an empty JSON object.
You can use the NEW JSON constructor to construct a JSON document that is stored in a binary format;
however, if the result of this constructor is sent to a client, it will be sent as a CLOB which contains the text
representation of the binary data.
You can append a JSON dot notation reference to the end of a constructor expression as described in JSON
Dot Notation (Entity Reference).
NEW JSON();
{"hello":"world"}
Transform Complex
Group Data Type Primary Type Default Format
Transform Complex
Group Data Type Primary Type Default Format
You can use the TRANSFORM option in the CREATE PROFILE/MODIFY PROFILE or CREATE USER/
MODIFY USER statements to specify for a user the particular transform group that will be used for a given
data type.
Use the SET TRANSFORM GROUP FOR TYPE statement to change the active transform group in the
current session. You can use this statement multiple times for a data type to switch from one transform group
to another within the session. If the logon user already has transform settings, the statement modifies the
transform settings for the current session.
Note:
You cannot use CREATE TRANSFORM or REPLACE TRANSFORM to create new transforms
for complex data types (CDTs). You can only create new transforms for structured and distinct
user-defined types (UDTs).
Macro Description
Macro Description
For more information about these macros, see Teradata Vantage™ - SQL Data Definition Language Syntax
and Examples, B035-1144.
Related Information
• "CREATE PROFILE" and "MODIFY PROFILE" in Teradata Vantage™ - SQL Data Definition Language
Syntax and Examples, B035-1144
• "CREATE USER" and "MODIFY USER" in Teradata Vantage™ - SQL Data Definition Language Syntax
and Examples, B035-1144
• "SET TRANSFORM GROUP FOR TYPE" in Teradata Vantage™ - SQL Data Definition Language
Syntax and Examples, B035-1144
In addition, if the conversion fails, a NULL value is returned instead of an error. This is useful for handling
dirty data that may contain anomalies not matching the desired target data type.
Note:
When casting a JSON dot notation expression to the following data types and the conversion fails, an
error is returned instead of a NULL value:
• TIME or TIMESTAMP
• DATE
• LOB or CLOB
• UDT
If any truncation of data occurs as a result of the cast, a NULL value is returned.
Related Information:
JSON Data Type Syntax
Storage Format Conversions
Conversion Rules for JSON Storage Formats
Result:
> 1.E0
Result:
> ?
The following CAST statements specify an inline length for the resulting JSON type:
Examples
The following SELECT statements all return {"hello":"world"}, but as different data types and
storage formats:
JSON FNC functions and Java classes and methods are provided to enable a UDF, UDM, or external
stored procedure to access and set the value of a JSON parameter, or to get information about the JSON
type parameter. For information about these functions and methods, see Teradata Vantage™ - SQL
External Routine Programming, B035-1147.
This example shows an SQL definition for a UDF using parameter style SQL with a JSON parameter, and
a C function that shows the corresponding parameter list.
void MyJSONUDF (
JSON_HANDLE *json_handle,
VARCHAR_LATIN *result,
int *indicator_ary,
int *indicator_result,
char sqlstate[6],
SQL_TEXT extname[129],
SQL_TEXT specific_name[129],
SQL_TEXT error_message[257])
{
/* body function */
}
This example shows an SQL definition for a UDF using parameter style TD_GENERAL with a JSON
parameter, and a C function that shows the corresponding parameter list.
RETURNS VARCHAR(100)
NO SQL
PARAMETER STYLE TD_GENERAL
DETERMINISTIC
LANGUAGE C
EXTERNAL NAME 'CS!MyJSONUDF2!MyJSONUDF2.c!F!MyJSONUDF2';
void MyJSONUDF2 (
JSON_HANDLE *json_handle,
VARCHAR_LATIN *result,
char sqlstate[6]
{
/* body function */
}
This example shows an SQL UDF with a RETURN expression that evaluates to JSON type.
This UDF shows the use of dot notation syntax to retrieve the value of "thenum" from the JSON string.
COLLATION INVOKER
INLINE TYPE 1
RETURN string.thenum;
Result:
Result:
test_udf(y1)
---------------------------------------------------------------------------
10
Result:
test_udf(y1)
---------------------------------------------------------------------------
10
Result:
test_udf(y1)
---------------------------------------------------------------------------
10
This UDF shows the use of dot notation syntax to retrieve the value of "name.firstname" from the
JSON string.
Result:
Result:
test_udf(y1)
---------------------------------------------------------------------------
abc
Result:
test_udf(y1)
---------------------------------------------------------------------------
abc
Result:
test_udf(y1)
---------------------------------------------------------------------------
abc
This UDF shows the use of dot notation syntax to retrieve the value of "name.firstname.lastname" from
the JSON string.
Result:
Result:
test_udf(y1)
---------------------------------------------------------------------------
abc
Result:
test_udf(y1)
---------------------------------------------------------------------------
abc
Result:
test_udf(y1)
---------------------------------------------------------------------------
abc
Example: JSON Type Parameter and Return Type For a Java UDF
This example shows a Java UDF defined with a JSON type input parameter and return type.
NO EXTERNAL DATA
PARAMETER STYLE JAVA
NOT DETERMINISTIC
CALLED ON NULL INPUT
EXTERNAL NAME 'JSON_JAR:JSONUDF.json_func1(java.sql.Clob)
returns java.sql.Clob';
This example shows an SQL definition for an external stored procedure using parameter style SQL with
an IN JSON parameter, and a C function that shows the corresponding parameter list.
VARCHAR_LATIN *result,
int *indicator_ary,
int *indicator_result,
char sqlstate[6],
SQL_TEXT extname[129],
SQL_TEXT specific_name[129],
SQL_TEXT error_message[257])
{
/* body function */
}
This example shows an SQL definition for an external stored procedure using parameter style
TD_GENERAL with an IN JSON parameter, and a C function that shows the corresponding parameter list.
void myJSONXSP2 (
JSON_HANDLE *json_handle,
VARCHAR_LATIN *result,
char sqlstate[6])
{
/* body function */
}
This example shows a Java external stored procedure defined with a JSON type IN parameter.
NO SQL
PARAMETER STYLE JAVA
EXTERNAL NAME 'JSON_JAR:JavaXSP.json_proc1(java.sql.Clob,java.sql.Clob[])';
The following statement defines a stored procedure with an IN parameter that is a JSON type with a
maximum length of 100.
The following statement defines a stored procedure with an OUT parameter that is a JSON type with a
maximum length of 100.
The following stored procedure contains local variables, local1 and local2, that are of JSON type with a
maximum length of 100. The local1 variable is initialized using the NEW JSON constructor method.
Note:
You cannot create methods for the JSON type. The only methods a JSON type can use are those that
are automatically created by Vantage. Therefore, you cannot specify a JSON type name in the FOR
clause of a CREATE/REPLACE METHOD statement.
JSON FNC functions and Java classes and methods are provided to enable a UDF, UDM, or external
stored procedure to access and set the value of a JSON parameter, or to get information about the JSON
type parameter. For information about these functions and methods, see Teradata Vantage™ - SQL
External Routine Programming, B035-1147.
The example shows an SQL definition for a UDM with a JSON parameter, and a C function that defines
the method.
void JSONMethod (
UDT_HANDLE *someUdt,
JSON_HANDLE *jsonval,
INTEGER *result,
int *indicator_this,
int *indicator_aryval,
int *indicator_result,
char sqlstate[6],
SQL_TEXT extname[129],
SQL_TEXT specific_name[129],
SQL_TEXT error_message[257])
{
/* body function */
}
The example shows an SQL definition for a UDM using parameter style TD_GENERAL with a JSON
parameter, and a C function that defines the method.
void JSONMethod (
UDT_HANDLE *someUdt,
JSON_HANDLE *jsonval,
INTEGER *result,
char sqlstate[6])
{
/* body function */
}
This example shows how to create a structured user-defined type (UDT) which has a JSON type attribute.
The routines in this example are created in the SYSUDTLIB database. Therefore, the user must have the
UDTMETHOD privilege on the SYSUDTLIB database.
SQL Definition
This section shows the SQL DDL statements necessary to create the structured UDT.
CREATE TYPE judt AS (Att1 JSON(100000) CHARACTER SET UNICODE) NOT FINAL
CONSTRUCTOR METHOD judt (p1 JSON(100000) CHARACTER SET UNICODE)
RETURNS judt
SELF AS RESULT
SPECIFIC judt_cstr
LANGUAGE C
PARAMETER STYLE TD_GENERAL
RETURNS NULL ON NULL INPUT
DETERMINISTIC
NO SQL;
The following statement associates the tosql and fromsql transform routines with the judt UDT.
NO SQL
PARAMETER STYLE SQL
RETURNS NULL ON NULL INPUT
DETERMINISTIC
LANGUAGE C
EXTERNAL NAME 'CS!judt_order!judt_order.c!F!judt_order';
C Source Files
This section shows the C code for the methods and functions created in the previous section. This is just
sample code so there is no meaningful logic in the tosql or Ordering functions. However, based on the
examples for the Constructor and fromsql routines, you can enhance the previous routines to perform the
necessary functions.
judt_constructor.c
int inNumLobs= 0;
FNC_GetJSONInfo(*file1,&inputMaxLength,&inputCharSet,&inNumLobs);
if (inNumLobs == 0)
{
FNC_GetInternalValue(*file1,input1,buffer_size, &actualInputLength);
FNC_SetStructuredAttribute(*resultUdt, "Att1", input1, 0,
actualInputLength);
}
else
{
LOB_LOCATOR inLOB;
LOB_RESULT_LOCATOR outLob;
LOB_CONTEXT_ID id;
FNC_LobLength_t readlen, writelen;
int trunc_err = 0;
FNC_GetJSONInputLob(*file1,&inLOB);
FNC_GetStructuredResultLobAttribute(*resultUdt, "Att1", &outLob);
judt_fromsql.c
BYTE temp[buffer_size];
if (*inNull != -1)
{
FNC_LobLength_t readlen, writelen;
attribute_info_t attrInfo;
FNC_GetStructuredAttributeInfo(*udt,0,sizeof(attrInfo), &attrInfo);
if (attrInfo.lob_length == 0)
{
FNC_GetStructuredAttribute(*udt, "Att1", temp, buffer_size,
&nullIndicator, &length);
readlen = length;
FNC_LobAppend(*result, temp, readlen, &writelen);
}
else
{
LOB_LOCATOR inLob;
LOB_CONTEXT_ID id;
int trunc_err = 0;
int remBufSize = buffer_size;
BYTE *input1Ptr = temp;
readlen = 0;
FNC_GetStructuredInputLobAttribute(*udt, "Att1", &nullIndicator,
&inLob);
FNC_LobOpen(inLob, &id, 0, 0);
length = FNC_GetLobLength(inLob);
FNC_LobClose(id);
}
*outNull = 0;
}
else *outNull = -1;
}
judt_tosql.c
judt_order.c
void judt_order (
UDT_HANDLE *UDT,
INTEGER *result,
int *indicator_udt,
int *indicator_result,
char sqlstate[6],
SQL_TEXT extname[129],
SQL_TEXT specific_name[129],
SQL_TEXT error_message[257])
{
/* Read out as much data as necessary, using either
FNC_GetStructuredAttribute or FNC_GetStructuredInputLobAttribute + LOB
FNC routines, following the example in judt_fromsql. Then use this data
to make the determination about the value of this instance in terms of
ordering. */
}
Result:
id j1
----------- ----------------------------------------------------------
1 {"name":"Cameron"}
2 {"name":"Melissa"}
Syntax
{ object | array }
Syntax Elements
object
{ [ name/value_pair [,...] ] }
array
[ [ value [,...] ] ]
Note:
You must type the colored or bold brackets.
name/value_pair
string
A JSON string literal enclosed in double quotation marks.
number
A JSON numeric literal.
Note:
A nested object or array counts against this limit.
For details about the JSON_MaxDepth field, see “DBS Control” in Teradata Vantage™ - Database
Utilities, B035-1102.
• You can specify exponential numbers using scientific notation. The range of valid numbers is between
-1e308 and 1e308, noninclusive. Exponents can have a value between -308 and 308, noninclusive. Any
number or exponent specified outside of the valid range, whether explicitly stated or understood to be
outside of that range based on the value of the mantissa, will result in an error.
Note:
Data cannot be imported to or exported from the database in the UBJSON format.
String Encodings
Both BSON and UBJSON encode strings in the UTF8 character set. However, UTF8 is not supported as a
character set inside Vantage. If data is imported to the database in JSON text format and is stored in one
of the binary storage formats, conversion is performed as described in Storage Format Conversions.
All character string data is converted from the source data (either in LATIN or UNICODE) to its UTF8
equivalent. Any errors encountered result in a failed operation. No errors are ignored.
Similarly, when data which was stored as BSON or UBJSON is exported as JSON text, all strings encoded
as UTF8 are converted to the appropriate character set (LATIN or UNICODE), and any errors encountered
result in a failed operation.
If data stored in one of the binary storage formats cannot be exported as JSON text due to a character
translation error, you can export it as BSON using one of the instance methods or casts of the JSON type.
Note:
You cannot use a JSON type column in an index definition.
• You can use the ALTER TABLE statement to add, drop, or rename a JSON type column.
• You can use ALTER TABLE to change the maximum length and inline storage length of a JSON column
subject to the following restrictions:
◦ If the existing JSON column type is a LOB type, you can only change the maximum length to a
larger value.
◦ If the existing JSON column type is a non-LOB type, the newly changed data type must remain
a non-LOB type, and the new maximum length and inline length values must be greater than the
old values.
• You cannot use ALTER TABLE to change the storage format of an existing column.
If you want to change the storage format of a JSON column, you must drop the column and add the column
back specifying the new storage format.
For details about CREATE TABLE and ALTER TABLE, see Teradata Vantage™ - SQL Data Definition
Language Syntax and Examples, B035-1144.
Note:
Using ALC together with block-level compression (BLC) may degrade performance, so this practice
is not recommended. For more information on compression use cases and examples, see Teradata
Orange Book Block-Level Compression in Teradata:
1. Go to https://support.teradata.com.
2. Log in.
For more information about compression functions, see Teradata Vantage™ - SQL Operators and
User-Defined Functions, B035-1210.
For information about the COMPRESS and DECOMPRESS phrases, see Teradata Vantage™ - Data Types
and Literals, B035-1143.
From BSON to Text The BSON data is converted to the character set of the column. In BSON, strings
are stored in UTF8, so if the data contains a character that is not in the repertoire of
the target character set, a translation error is reported.
BSON contains extensions to the base JSON data types (such as binary data and
dates). These extended data types are treated as strings in the resulting JSON
text document.
From UBJSON The UBJSON data is converted to the character set of the column. In UBJSON,
to Text strings are stored in UTF8, so if the data contains a character that is not in the
repertoire of the target character set, a translation error is reported.
From Text to BSON JSON text data is converted to the BSON format. All strings are encoded in UTF8,
even if the source JSON text was in the LATIN character set. No extended data
types are used in the resulting BSON document.
Conversion of
Storage Format Description
From UBJSON UBJSON data is converted to the BSON format. No extended data types are used
to BSON in the resulting BSON document.
From Text JSON text data is converted to the UBJSON format. All strings are encoded in
to UBJSON UTF8, even if the source JSON text was in the LATIN character set.
From BSON BSON data is converted to the UBJSON format. BSON contains extensions to the
to UBJSON base JSON data types (such as binary data and dates). These extended data types
are treated as strings in the resulting UBJSON document.
Note:
The root of a JSON document when stored as BSON is always assumed to be an object. Furthermore,
arrays are stored as key/value pairs where the key is the array index of the element.
[ "a" , "b" ]
When this BSON document is converted to text, you will get that object-like representation. This is due
to the BSON assumption that the root of the document is an object. This is a limitation of the BSON
specification. Nested arrays do not suffer from this limitation because they are represented identically, but
preceded by an indicator byte to signify that the data represents an array. Therefore, when converting back
to JSON text, the keys are not printed out.
Consider the following document:
The nested array is treated properly as an array, so that if you convert this text to BSON and then back to
text, you will get the exact same document as follows:
Note that when searching for a value, the JSONPath query does not change whether the data is stored
as an array or as an object. For example, consider the following documents stored as an array and as
an object:
[ "a" , "b" ]
When searching these documents for the value "a", the following query succeeds on both documents:
The reason is that JSONPath normalizes the request query. One of the normalizations involves removing
the square brackets, so when evaluating the query, it is not known if the user wanted to search for a key
called "0" or an element at index 0 of an array. JSONPath will do the following:
1. Search for the key.
2. If the search fails, and the value being searched for is a positive integer and we are in an array, then
look for an element at the specified index.
Therefore, both cases will return "a" when searched.
In summary, the limitation only applies when reading the BSON data and converting it back to text. The
resulting text document may look a little different, but it can be searched in the same way.
Related Information:
Conversion Rules
To re-enable validation for the current session, use the following statement:
If JSON validation is disabled at either the session or system level, the following will occur:
• Even with validation disabled, JSON text imported to the database and stored as JSON text is
minimally validated to ensure that the opening and closing braces and brackets of the JSON document
properly match. For example, '{' matches with '}' and '[' matches with ']'. Failure to pass this minimal
validation will result in an error.
• JSON text imported to the database and stored as one of the binary storage formats is still validated.
Vantage does not allow an improper JSON document to be converted to one of the binary storage
formats. In this case, the settings for the JSON IGNORE ERRORS session attribute or the DBS
Control DisableJSONValidation field have no impact.
• The maximum length specified for a JSON column that uses a binary storage format must cover the
length of the data in its binary format. If validation is disabled and the binary data inserted exceeds the
defined length of the column, an error is reported.
If the binary data inserted fits within the defined length of the column, but the serialized (text) format
exceeds the length, an error is reported when trying to access the data as text.
• JSON data imported to the database in its BSON format and stored in the BSON format is accepted
without any validation. Because validation is completely disabled, no errors are reported for any
issues encountered.
• JSON data cannot be imported to the database in UBJSON format; therefore, the UBJSON data
is always validated when it is converted from its source format because Vantage cannot convert
invalid data.
BSON is the default document storage scheme for MongoDB. There are additional restrictions that dictate
whether or not BSON data is valid in MongoDB. These restrictions are as follows:
• A user document element name cannot begin with a '$' character.
• A user document element name cannot have a '.' character in the name.
• The element name _id is reserved for use as a primary key id, but you can store anything that is unique
in that field.
Even with validation enabled, this level of validation needed for MongoDB is not performed when inserting
data into a JSON column which stores its data as BSON. However, the AsBSON method provides an
optional parameter that allows you to perform this strict validation.
You can use the BSON_CHECK function to verify that JSON data in BSON format is compliant with BSON
syntax. If the data is valid, the BSON_CHECK function returns OK. The BSON_CHECK function also has
an optional parameter that allows you to specify whether or not to perform a strict validation which verifies
that the BSON syntax is valid for MongoDB.
You can use the JSON_CHECK function to verify that a JSON string is compliant with JSON syntax. If the
JSON string is valid, the JSON_CHECK function returns OK.
Related Information:
Maximum Length of a JSON Instance
AsBSON
JSON_CHECK
BSON_CHECK
The following shows a sample USING clause generated by the Teradata PT SQL Inserter operator:
USING COL1_INT(INTEGER),
COL2_CHAR(CHAR(10)),
COL3_JSON(CLOB(1000)),
COL4_BIGINT(BIGINT),
COL5_JSON(CLOB(16776192) AS DEFERRED),
COL6_VARCHAR(VARCHAR(20))
INSERT INTO target_table
For more information about the Teradata PT load utility, see the following documents:
In this example, the transform group settings for the user specify to use the TD_JSON_VARBYTE
transform group. The following shows how you can load JSON data into the JSON column of the
jsn_byte_bson table.
.logon NODEID/dr171210_vb,dr171210_vb;
You can insert data into a UBJSON-formatted column using the following:
• CAST statement
To/from CHAR/VARCHAR/CLOB (in JSON text format or any other JSON format).
• NEW JSON Constructor
The input to the constructor can be CHAR/VARCHAR/CLOB (in JSON text format).
Related Information:
Casting JSON Data
The example inserts a JSON string into a table that contains a JSON column.
Note:
If the string is not formatted correctly, an error will be reported.
Note:
If the JSON data is too large to fit in the column an error is reported.
*** Failure 7548: Data too large for this JSON instance.
name_or_index_list |
slice
} [...]
Syntax Elements
object_member
{.|..} name
array_element
Note:
You must type the colored or bold brackets.
wildcard
{.|..} *
name_or_index_list
slice
Note:
You must type the colored or bold brackets.
name
{ nonreserved_word | "string_literal" }
list_value
{ name | integer }
JSON_expr
An expression that evaluates to a JSON data type.
Object Member
Consists of a descent operator followed by a Name syntax element.
A descent operator is one of the following:
• A child operator '.'
• A recursive descent operator '..'
Name
A Teradata Vantage nonreserved word or a string literal enclosed in double quotation marks.
Array Element
Consists of an optional recursive descent operator '..' followed by an index value enclosed
in brackets.
An index value is one of the following:
• An unsigned INTEGER value
• A wildcard operator '*'
Wildcard
Consists of a descent operator followed by the wildcard character '*'.
A descent operator is one of the following:
• A child operator '.'
• A recursive descent operator '..'
The wildcard operator can be used both in reference to named and indexed items.
Examples:
• Name list, such as [a,b,c]
• Index list, such as [0,3,5]
Slice
Consists of an optional recursive descent operator '..' followed by 2 or 3 colon-separated
INTEGER values enclosed in brackets.
For example, [0:5:3]
The syntax for JSON dot notation, a Teradata Vantage ARRAY type element reference, and a fully or
partially qualified column reference is similar. This creates potential ambiguity in the following cases:
1. Ambiguity between a Teradata Vantage ARRAY type element reference and a JSON type
array reference
2. Ambiguity between a fully or partially qualified column reference and a JSON type dot
notation reference
When there is ambiguity, the default interpretation of the syntax is as follows:
• For case 1: The syntax is interpreted as a Teradata Vantage ARRAY type element reference.
• For case 2: The syntax is interpreted as a fully or partially qualified column reference.
Handling of these potential ambiguities is described in more detail in the following sections.
To ensure that a JSON dot notation reference is not ambiguous, fully qualify a JSON column being
referenced. Alternatively, you can use the JSONExtract, JSONExtractValue, or JSONExtractLargeValue
methods to retrieve entities of a JSON instance.
When parsing a request that features syntax matching both a Teradata Vantage ARRAY type element
reference and a JSON type array reference, the following logic is employed:
1. The syntax is first treated as an element reference on a Teradata Vantage ARRAY type.
2. If it is determined that the source is not an ARRAY type, TeradataVantage checks to see if it is an
instance of the JSON type.
3. If it is a JSON type instance, Vantage interprets the syntax as a JSON type array reference.
4. If it is not a JSON type instance, an error is returned indicating an incorrect array element reference.
When parsing a request that features syntax matching both a fully or partially qualified column reference
and a JSON type dot notation reference, the following logic is employed.
Example
SELECT jsonCol.name
FROM test.jsonTable
WHERE id=1;
Result:
jsonCol.name
------------
Cameron
Example
SELECT jsonCol.numbers[1]
FROM test.jsonTable
WHERE id=1;
Result:
jsonCol.numbers[1]
------------------
2
Example
Result:
Related Information:
Setting Up the JSON Dot Notation Examples
Example
In the following query, T.jsonCol and T.id are interpreted as column references.
Result:
id jsonCol
--------------
1 {"name" : "Cameron", "numbers" : [1,2,3,[1,2]]}
2 {"name" : "Cameron", "name" : "Lewis"}
Example
In the following query, jsonCol.name is interpreted as JSON dot notation.
Result:
id jsonCol.name
-------------------
1 Cameron
Example
The following query returns an error because there is more than one source table with a JSON column
named jsonCol.
SELECT jsonCol.name
FROM test.jsonTable, test.jsonTable2;
Result:
Example
The following query shows a JSON dot notation reference specified as a fully qualified column reference.
Result:
id jsonTable.jsonCol.name
----------------------------------
1 Cameron
Example
The following shows incorrect JSON dot notation specified as a fully qualified column reference.
SELECT test.jsonTable.id.name
FROM test.jsonTable
WHERE id=1;
*** Failure 3706 Syntax error: Invalid use of JSON entity reference syntax on
non-JSON type.
Example
In the following query, jsonTable.jsonCol.name is a JSON dot notation reference that looks like a
database.table.column reference.
Result:
id jsonTable.jsonCol.name
-----------------------------
1 Cameron
Example
Incorrect JSON dot notation reference
SELECT jsonTable.id.name
FROM test.jsonTable
WHERE id=1;
Result:
Example
In the following query, jsonCol.name."first" is interpreted as a dot notation reference on the jsonCol
column of the source table, test.jsonTable.
Result:
id jsonCol.name.first
-------------------------
1 ?
2 ?
3 Cameron
Example
In the following query, the reference to jsonCol is ambiguous because both source tables have a JSON
column named jsonCol.
Example
In this example, jsonTable2 is in the current database and it has a JSON column called jsonCol, so
jsonTable2.jsonCol.name is interpreted as JSON dot notation.
Result:
id jsonCol.name
------------------------------
1 Cameron
Related Information:
Setting Up the JSON Dot Notation Examples
This example shows a problematic JSON dot notation reference that finds multiple results and returns
an error. If you want a list of values to be returned instead, you must specify this behavior using the
SET SESSION DOT NOTATION...ON ERROR statement or the DotNotationOnErrorCondition DBS
Control field.
Example
Result:
id jsonCol.numbers
------------------------
1 *** ERROR MULTI RESULT ***
2 ? /* There are no numbers in this JSON */
Related Information:
Setting Up the JSON Dot Notation Examples
JSONPath Request
JSONPath notation lets you specify a portion of a JSON document. You can use a JSONPath string to
identify any portion of the document, such as an object (including all nested objects and arrays), a single
name/value pair, an array, a specific element of an array, or a value. Several JSON functions and methods
accept a JSONPath string as input.
The principles of JSONPath are analogous to those of XPath for XML.
Note:
Vantage does not provide complete validation of the syntax, so malformed query strings can produce
undesirable results. Vantage only provides the following validations:
• Validate the presence of the '$' root character.
• Validate the omission of the following characters:
◦ Any ';' characters
◦ Three or more consecutive '.' characters. For example: '…'
. [ $ [children [...]] ] .
Syntax Elements
$
The root object or element.
children
child_specification
{ * | name_string [ options ] }
options
Note:
You must type the colored or bold brackets.
index
{ * |
integer [ : | , integer | : integer : integer ] |
: integer
}
expression
In this context, LENGTH is the length of the current JSON array, equal to the number of
elements in the array.
filter
number_comparison
@
The current object or element.
integer
A signed integer.
element_string
A string specifying the name of an element.
=~ string
String comparison expression.
{
"customer" : "CustomerName",
"orderID" : 3,
"price" : "$100000.00",
"items" :
[
{ "ID" : 1, "name" : "disk", "amt" : 10 },
{ "ID" : 2, "name" : "RAM", "amt" : 20 },
{ "ID" : 3, "name" : "monitor", "amt" : 30 },
{ "ID" : 4, "name" : "keyboard", "amt" : 40 },
{ "ID" : 5, "name" : "camera", "amt" : 50 },
{ "ID" : 6, "name" : "button", "amt" : 60 },
{ "ID" : 7, "name" : "mouse", "amt" : 70 },
{ "ID" : 8, "name" : "pen", "amt" : 80 }
]
}
@ The current object/element $.items[(@. The last item in the order. {"ID":8,"name":"pen","amt":80}
length-1)] The use of the 'length' keyword in this
context is interpreted as the length of
the current JSON array and is treated
as a property of the JSON array. This is
only interpreted in this manner if 'length'
occurs immediately after the '@.' syntax.
If the word 'length' is found later in the
expression (for example, '@.firstChild.
length'), it is interpreted as the name of a
child of some entity, not as a property of
that entity.
[start:end: Array slice operator $.items[0:4:2] All items from 1-5 (not inclusive on the [{"ID":1,"name":"disk","amt":10},
step] If you do not specify start, end index) by a step of 2 (That is, items 1 {"ID":3,"name":"monitor","amt":
the default is the first index. and 3) 30}]
If you do not specify end,
the default is the last index.
If you do not specify step,
the default is a step of 1.
?( ) Applies a filter $.items[?(@. Filter all items of which a quantity less [{"ID":1,"name":"disk","amt":10},
(script) expression amt<50)] than 50 was ordered {"ID":2,"name":"RAM","amt":20},
{"ID":3,"name":"monitor","amt":
30},
{"ID":4,"name":"keyboard","amt":
40}]
() Script expression, using the $.items[(@. The last item in the order {"ID":8,"name":"pen","amt":80}
underlying script engine length-1)]
Note:
You cannot include a JSON column in the ORDER BY, HAVING or GROUP BY clauses of a
SELECT statement.
In the SELECT or WHERE clause, you can add a JSON entity reference to the end of a column reference
or any expression which evaluates to a JSON type.
Result:
Result:
eno edata
--------
1 '{"name" : "Cameron", "phoneNumber" : 9595552612}'
Note:
The JSONExtractValue and JSONExtractLargeValue methods only extract a single scalar value or a
JSON null. If more than one value matches the JSONPath query expression, a warning and an error
message string indicating that multiple results were found are returned.
The search is optimized in that it does not always need to search the entire document. However, this means
that the following scenario is possible.
1. Disable JSON validation
2. Insert malformed JSON data. For example, something similar to this: {"name":"Cameron" 123456}
3. Enable JSON validation
4. Attempt to extract the value of the key "name"
In this case, the correct result of "Cameron" is returned, because the result was found before passing
through the entire document and reaching the malformed data. If any malformed data is encountered during
the search, before the result is reached, a syntax error is reported.
Related Information:
Validating JSON Data
JSON Methods, Functions, External Stored Procedures, and Table Operators
JSON Dot Notation (Entity Reference)
Retrieving JSON Data Using SELECT
Result:
The same query using JSONExtractValue instead of dot notation also returns a warning and an error
message string.
Result:
You can use the JSONExtract method to extract multiple values. The same query using the JSONExtract
method succeeds and returns the multiple values in a JSON array.
Result:
{
"customer" : "CustomerName",
"orderID" : 3,
"price" : "$100000.00",
"items" :
[
{ "ID" : 1, "name" : "disk", "amt" : 10 },
{ "ID" : 2, "name" : "RAM", "amt" : 20 },
{ "ID" : 3, "name" : "monitor", "amt" : 30 },
{ "ID" : 4, "name" : "keyboard", "amt" : 40 },
{ "ID" : 5, "name" : "camera", "amt" : 50 },
{ "ID" : 6, "name" : "button", "amt" : 60 },
{ "ID" : 7, "name" : "mouse", "amt" : 70 },
{ "ID" : 8, "name" : "pen", "amt" : 80 }
]
}
"customer" : "CustomerName",
"orderID" : 3,
"price" : "$100000.00",
"items" :
[
{ "ID" : 1, "name" : "disk", "amt" : 10 },
{ "ID" : 2, "name" : "RAM", "amt" : 20 },
{ "ID" : 3, "name" : "monitor", "amt" : 30 },
{ "ID" : 4, "name" : "keyboard", "amt" : 40 },
{ "ID" : 5, "name" : "camera", "amt" : 50 },
{ "ID" : 6, "name" : "button", "amt" : 60 },
{ "ID" : 7, "name" : "mouse", "amt" : 70 },
{ "ID" : 8, "name" : "pen", "amt" : 80 }
]
}',
'{
"customer" : "CustomerName",
"orderID" : 3,
"price" : "$100000.00",
"items" :
[
{ "ID" : 1, "name" : "disk", "amt" : 10 },
{ "ID" : 2, "name" : "RAM", "amt" : 20 },
{ "ID" : 3, "name" : "monitor", "amt" : 30 },
{ "ID" : 4, "name" : "keyboard", "amt" : 40 },
{ "ID" : 5, "name" : "camera", "amt" : 50 },
{ "ID" : 6, "name" : "button", "amt" : 60 },
{ "ID" : 7, "name" : "mouse", "amt" : 70 },
{ "ID" : 8, "name" : "pen", "amt" : 80 }
]
}',
'{
"customer" : "CustomerName",
"orderID" : 3,
"price" : "$100000.00",
"items" :
[
{ "ID" : 1, "name" : "disk", "amt" : 10 },
{ "ID" : 2, "name" : "RAM", "amt" : 20 },
{ "ID" : 3, "name" : "monitor", "amt" : 30 },
{ "ID" : 4, "name" : "keyboard", "amt" : 40 },
{ "ID" : 5, "name" : "camera", "amt" : 50 },
{ "ID" : 6, "name" : "button", "amt" : 60 },
{ "ID" : 7, "name" : "mouse", "amt" : 70 },
> ["disk","RAM","monitor","keyboard","camera","button","mouse","pen"]
A list of results is returned instead of an error string because the dot notation query includes the recursive
descent operator.
The following SELECT statements show the use of the wildcard operator (*):
> [1,"disk",10]
A list of results is returned instead of an error string because the dot notation query includes the
wildcard operator.
The following SELECT statements show the use of a named list operator:
> ["CustomerName",3]
A list of results is returned instead of an error string because the dot notation query includes a named
list operator.
The following SELECT statements show the use of an index list operator:
> [{"ID":1,"name":"disk","amt":10},{"ID":2,"name":"RAM","amt":20}]
A list of results is returned instead of an error string because the dot notation query includes an index
list operator.
The following SELECT statements show the use of a slice operator:
> [{"ID":1,"name":"disk","amt":10},{"ID":3,"name":"monitor","amt":30}]
A list of results is returned instead of an error string because the dot notation query includes a
slice operator.
• When ANY/SOME bound a dot notation query, all results are compared against the other operand, and
if ANY comparison results in true, the condition evaluates to true. For example:
The result is 1.
• When ALL bounds a dot notation query, all results are compared against the other operand, and if ALL
comparison results in true, the condition evaluates to true. For example:
The result is 1.
• When there is neither ANY nor SOME nor ALL bounding a dot notation query, the list of results is
compared against the other operand. For example:
The result is 1.
• When the method style syntax is used, the list of results is compared against the other operand.
For example:
The result is 1.
These rules apply to all dot notation queries regardless of data type. If this atomized evaluation is not
desired, comparison of an entire list may be performed by omitting the ANY/SOME/ALL clause from the
condition, or using the method style syntax.
Note:
Column references may only be scoped to tables in the current database or any tables in the query.
You will get an error if you try to qualify a column to the database level. For example, the following
query fails:
SELECT jsonCol02.customer
FROM jsonEnhancedDotNotationTbl
WHERE 'disk'
= ANY(databasename.jsonEnhancedDotNotationTbl.jsonCol02.items..name);
Result:
SELECT jsonCol01.customer
FROM jsonEnhancedDotNotationTbl
WHERE 'disk' = ANY(jsonCol01.items..name);
Result:
> CustomerName
SELECT jsonCol02.customer
FROM jsonEnhancedDotNotationTbl
WHERE 'disk' = ANY(jsonCol02.items..name);
Result:
> CustomerName
SELECT jsonCol03.customer
FROM jsonEnhancedDotNotationTbl
WHERE 'disk' = ANY(jsonCol03.items..name);
Result:
> CustomerName
SELECT jsonCol04.customer
FROM jsonEnhancedDotNotationTbl
WHERE 'disk' = ANY(jsonCol04.items..name);
Result:
> CustomerName
SELECT jsonCol01.customer
FROM jsonEnhancedDotNotationTbl
WHERE 'disk' = ALL(jsonCol01.items..name);
Result:
SELECT jsonCol01.customer
FROM jsonEnhancedDotNotationTbl
WHERE 40 < ANY(jsonCol01.items..amt);
Result:
> CustomerName
SELECT jsonCol02.customer
FROM jsonEnhancedDotNotationTbl
WHERE 40 < ANY(jsonCol02.items..amt);
Result:
> CustomerName
SELECT jsonCol03.customer
FROM jsonEnhancedDotNotationTbl
WHERE 40 < ANY(jsonCol03.items..amt);
Result:
> CustomerName
SELECT jsonCol04.customer
FROM jsonEnhancedDotNotationTbl
WHERE 40 < ANY(jsonCol04.items..amt);
Result:
> CustomerName
Note:
You can only update the JSON column, not individual portions of the JSON instance. Also, when
referencing a JSON column in its entirety, the input format is the same as the INSERT statement.
Note:
The following MERGE statement clauses are supported on JSON functions and methods:
▪ The ON clause can be used to condition on a JSON instance.
▪ The USING clause can be used to select the JSON column value.
UPDATE my_table
SET edata = NEW JSON('{"name" : "Justin"}')
WHERE edata.JSONExtractValue('$.name') = 'Cameron';
edata
-----
{"name" : "Justin"}
UPDATE my_table
SET edata = '{"name" : "George"}';
Result: To see the name has been updated, run: SELECT edata FROM my_table;
edata
-----
{"name" : "George"}
To see the result of the query, run: SELECT * FROM my_table ORDER BY eno;
eno edata
---------
1 {"name" : "Kevin", "phoneNumber" : 9595552613}
2 {"name" : "Cameron", "phoneNumber" : 9595552612}
To see the result of the query, run: SELECT * FROM my_table ORDER BY eno;
eno edata
-----------
1 {"name" : "Kevin", "phoneNumber" : 9595552613}
2 {"name" : "Cameron", "phoneNumber" : 9595552612}
2 {"name" : "Mike", "phoneNumber" : 9595552614}
Result:
The following statement fails because LOB types cannot be part of an index:
You can also create a join index that contains extracted portions of a JSON instance. You can do this with
non-LOB and LOB JSON types.
Use the following to extract portions of the JSON data:
• JSON dot notation syntax
• JSONExtractValue or JSONExtractLargeValue methods
The example shows how to create a JOIN INDEX with portions of a JSON instance specified using JSON
dot notation.
Related Information:
Setting Up Join Index Examples
The example creates a JOIN INDEX with portions of a JSON instance extracted by JSONExtractValue.
Related Information:
Setting Up Join Index Examples
Note:
You cannot collect statistics on an entire JSON instance.
Related Information:
JSON Dot Notation (Entity Reference)
JSONExtractValue and JSONExtractLargeValue
The example shows how to use COLLECT STATISTICS with portions of a JSON instance specified using
JSON dot notation.
Related Information:
Setting Up Collect Statistics Examples
The example uses COLLECT STATISTICS with portions of a JSON instance extracted
by JSONExtractValue.
Related Information:
Setting Up Collect Statistics Examples
Methods
AsBSON
Returns the BSON representation of the specified JSON instance.
AsJSONText
Returns the text representation of the specified JSON instance.
Combine
Takes two JSON documents and combines them into a JSON document structured as an
array or a JSON document structured as an object.
ExistValue
Allows you to specify a name or path in JSONPath syntax to determine if that name or path
exists in the JSON document.
JSONExtract
Extracts data from a JSON instance. The desired data is specified in a JSONPath expression.
The result is a JSON array composed of the values found, or NULL if there are no matches.
JSONExtractValue
Allows you to retrieve the text representation of the value of an entity in a JSON instance,
specified using JSONPath syntax.
JSONExtractValue extracts a single scalar value or a JSON null. The returned value is a
VARCHAR with a default length of 4K, but this can be increased up to 32000 characters (not
bytes) using the DBS Control JSON_AttributeSize field.
JSONExtractLargeValue
Functions the same as JSONExtractValue, except for the return size and type. For LOB-based
JSON objects, this method returns a CLOB of 16776192 characters for CHARACTER SET
LATIN or 8388096 characters for CHARACTER SET UNICODE.
KeyCount
Returns the number of keys in a JSON document.
Metadata
Returns metadata about a JSON document such as the number of values that are Objects,
Arrays, Strings, or Numbers.
StorageSize
Returns the number of bytes needed to store the input JSON data in the specified
storage format.
Functions
ARRAY_TO_JSON
Allows any Vantage ARRAY type to be converted to a JSON type composed of an array.
BSON_CHECK
Checks a string for valid BSON syntax and provides an informative error message about the
cause of the syntax failure if the string is invalid.
DataSize
Returns the data length in bytes of a JSON instance.
GeoJSONFromGeom
Converts an ST_Geometry object into a JSON document that conforms to the
GeoJSON standards.
GeomFromGeoJSON
Converts a JSON document that conforms to the GeoJSON standards into an
ST_Geometry object.
JSON_AGG
This aggregate function takes a variable number of input parameters and packages them into
a JSON document.
JSON_COMPOSE
This scalar function takes a variable number of input parameters and packages them into a
JSON document. This function provides a complex composition of a JSON document when
used in conjunction with the JSON_AGG function.
JSON_CHECK
Checks a string for valid JSON syntax and provides an informative error message about the
cause of the syntax failure if the string is invalid.
JSONGETVALUE
Extracts a value from a JSON object as a specific type.
JSONMETADATA
An aggregate function that returns metadata about a set of JSON documents.
NVP2JSON
Converts a string of name-value pairs to a JSON object.
Table Operators
JSON_KEYS
Parses a JSON instance, from either CHAR or VARCHAR input and returns a list of
key names.
JSON_PUBLISH
Compose a JSON data type instance or instances from a variety of data sources, that is
anything that can be referenced in an SQL statement. It can publish JSON data types of any
storage format.
JSON_TABLE
Creates a temporary table based on all, or a subset, of the data in a JSON object.
AsBSON
The AsBSON method returns the BSON representation of the specified JSON instance.
A BLOB value representing the input JSON data in BSON format.
A Vantage NULL is returned if the JSON_expr argument is NULL.
AsBSON Syntax
JSON_expr.AsBSON ( [ 'STRICT' | 'LAX' ] )
Syntax Elements
JSON_expr
An expression that evaluates to a JSON data type.
The JSON data can be stored in any of the supported storage formats.
STRICT
The data is validated according to the BSON specification located at http://bsonspec.org/ and
the MongoDB restrictions.
The character string 'STRICT' is not case sensitive. For example, you can also specify 'strict'.
LAX
The data is validated according to the BSON specification located at http://bsonspec.org/.
This is the default behavior if you do not specify 'STRICT' or 'LAX'.
The character string 'LAX' is not case sensitive. For example, you can also specify 'lax'.
Usage Notes
You can use the AsBSON method to get the BSON representation of JSON data that is stored in another
storage format, such as text or UBJSON.
If the input JSON data is already stored as BSON, this method merely returns the data passed into it.
Result:
j1.AsBSON ()
--------------------------------------------
160000000268656C6C6F0006000000776F726C640000
Result:
j1.AsBSON ('STRICT')
--------------------------------------------
160000000268656C6C6F0006000000776F726C640000
Result:
*** Failure 7548 Dollar sign ('$') not permitted in a key of a BSON
document, according to the chosen validation scheme.
Error encountered at offset 4.
Result:
j1.AsBSON ('LAX')
--------------------------------------------
160000000268656C6C6F0006000000776F726C640000
17000000022468656C6C6F0006000000776F726C640000
AsJSONText
The AsJSONText method returns the text representation of the specified JSON instance.
A CLOB value representing the input JSON data in text format. The character set of the return value
is UNICODE.
A Vantage NULL is returned if the JSON_expr argument is NULL.
AsJSONText Syntax
JSON_expr.AsBSONText ()
Syntax Elements
JSON_expr
An expression that evaluates to a JSON data type.
The JSON data can be stored in any of the supported storage formats.
Usage Notes
You can use the AsJSONText method to get the JSON text representation of JSON data that is stored in
a binary format such as BSON or UBJSON.
If the input JSON data is already stored as text, this method merely returns the data passed into it.
Result:
j1.AsJSONText()
------------------
{"hello": "world"}
Combine
The Combine method takes two JSON documents (specified by the JSON expressions), combines them,
and returns a JSON document structured as an array or structured as an object.
Combine Syntax
JSON_expr.Combine ( JSON_expr [, { 'ARRAY' | 'OBJECT' } ] )
Syntax Elements
JSON_expr
An expression that evaluates to a JSON data type. For example, this can be a column in a
table, a constant, or the result of a function or method.
'ARRAY' or 'OBJECT'
Optional. Explicitly specifies the result type as 'ARRAY' or 'OBJECT'.
If 'ARRAY' is specified, the result is a JSON document structured as an array. If 'OBJECT' is
specified, the result is a JSON document structured as an object.
When specifying 'OBJECT', both of the JSON documents being combined must be JSON
objects; otherwise, an error is reported.
• If one of the JSON documents being combined is NULL, the result is a non-NULL JSON document.
• If both JSON documents being combined are NULL, the result is a
Vantage NULL.
COMBINE and CHARACTER SET
• If one of the JSON documents being combined has CHARACTER SET UNICODE, the resulting JSON
instance has text in CHARACTER SET UNICODE.
• If both JSON documents being combined has text in CHARACTER SET LATIN, the resulting JSON
instance has text in CHARACTER SET LATIN.
COMBINE and Storage Format
JSON data stored in any storage format may be combined with JSON data stored in any other format.
• If both JSON documents being combined are stored as text, the result is a JSON document stored as
text with the character set defined as in the previous section.
• If one of the JSON documents is stored as text and the other is stored in a binary format, the result is
a JSON document stored as text in CHARACTER SET UNICODE.
• If one of the JSON documents is stored as BSON and the other is stored as UBJSON, the result is a
JSON document stored in BSON format.
• If both documents are stored in UBJSON format, the result is a JSON document stored in
UBJSON format.
Combine Examples
Create and populate two tables containing JSON objects and INTEGER data. The examples in the
following sections refer to this table and data.
The example shows how to use the JSON Combine method to combine two JSON objects, one of
CHARACTER SET UNICODE and the other of CHARACTER SET LATIN, which results in a JSON
instance with text in CHARACTER SET UNICODE. This example also uses the 'OBJECT' parameter to
specify a JSON object as the result.
Note:
The example uses the table(s) created earlier.
Result: When combining LATIN and UNICODE, the result is a JSON instance with text in CHARACTER
SET UNICODE.
edata.Combine(edata2)
---------------------
Related Information:
Setting Up the Combine Examples
The example shows the result when two JSON objects are combined with both JSON objects
having CHARACTER SET UNICODE, which results in a JSON instance with text in CHARACTER
SET UNICODE.
Note:
The example uses the table(s) created earlier.
SELECT MA.edata.Combine(MB.edata)
FROM my_table AS MA, my_table2 AS MB
WHERE MA.eno < 3;
Result: When combining UNICODE and UNICODE, the result is a JSON instance with text in
CHARACTER SET UNICODE.
edata.Combine(edata)
--------------------
{"name" : "Cameron","name" : "Cameron"}
{"name" : "Cameron","name" : "Melissa"}
{"name" : "Melissa","name" : "Cameron"}
{"name" : "Melissa","name" : "Melissa"}
Related Information:
Setting Up the Combine Examples
The example shows how to use the JSON Combine method to combine two JSON objects of
CHARACTER SET LATIN.
Note:
The example uses the table(s) created earlier.
SELECT MA.edata2.Combine(MB.edata2)
FROM my_table AS MA, my_table2 AS MB
WHERE MA.eno < 3;
Result: The result is a JSON instance with text in CHARACTER SET LATIN.
edata2.Combine(edata2)
---------------------
{"name" : "Lewis","name" : "Lewis"}
{"name" : "Lewis","name" : "Lewis"}
{"name" : "Lewis","name" : "Lewis"}
{"name" : "Lewis","name" : "Lewis"}
Related Information:
Setting Up the Combine Examples
The example shows a JSON array combined with a JSON object; the result type is not specified.
Note:
The example uses the table(s) created earlier.
Result: The result of combining a JSON array with a JSON object is implicitly a JSON ARRAY.
edata.Combine(edata2)
---------------------
[1,2,3, {"name" : "Lewis"}]
Related Information:
Setting Up the Combine Examples
The example shows how to use the JSON Combine method with the 'ARRAY' parameter to explicitly set
the result to a JSON array.
Note:
The example uses the table(s) created earlier.
edata2.Combine(edata2)
----------------------
[{"name" : "Lewis"},{"name" : "Lewis"}]
[{"name" : "Lewis"},{"name" : "Lewis"}]
[{"name" : "Lewis"},{"name" : "Lewis"}]
[{"name" : "Lewis"},{"name" : "Lewis"}]
Related Information:
Setting Up the Combine Examples
Example: Combine Two JSON ARRAYs and Explicitly State the Result Is
a JSON ARRAY
The following examples show how to use the JSON Combine method with the 'ARRAY' parameter to
explicitly set the result to a JSON array.
The example specifies the 'ARRAY' parameter. Note, the data being combined are both JSON array
instances, so the result is implicitly a JSON ARRAY even if the 'ARRAY' parameter is not specified.
Note:
The example uses the table(s) created earlier.
edata.Combine(edata)
[1,2,3,1,2,3]
Related Information:
Setting Up the Combine Examples
Combine a JSON array and an expression that evaluates to a JSON array and specify 'ARRAY' as the
result. Note, the result is implicitly a JSON array even if the 'ARRAY' parameter is not specified.
Note:
The example uses the table(s) created earlier.
Result: The result is an array of several elements, with the last element an array itself.
edata.Combine(edata2)
[1,2,3,1,2,[3,4]]
Related Information:
Setting Up the Combine Examples
Example: Error - Combine Two JSON ARRAYs and State the Result Is a
JSON OBJECT
The example specifies the 'OBJECT' parameter; however, the data being combined are both JSON array
instances, so the result must be a JSON ARRAY. This example results in an error because 'OBJECT'
was specified.
Note:
The example uses the table(s) created earlier.
/*Error case*/
The example results in an error similar to this: *** Failure 7548: Invalid options for JSON
Combine method.
Related Information:
Setting Up the Combine Examples
ExistValue
The ExistValue method determines if a name represented by a JSONPath-formatted string exists in a
JSON instance.
ExistValue determines if the name specified by JSONPath_expr exists in the JSON instance specified
by JSON_expr.
ExistValue Syntax
JSON_expr.ExistValue (JSONPath_expr)
Syntax Elements
JSON_expr
An expression that evaluates to a JSON data type.
JSONPath_expr
A name in JSONPath syntax.
The name can be either UNICODE or LATIN, depending on the character set of the JSON
type that invoked this method. If the parameter character set does not match the character
set of the JSON type, Vantage attempts to translate the parameter character set to the correct
character set.
JSONPath_expr cannot be NULL. If the expression is NULL an error is reported.
ExistValue Examples
The following query results in 'True' if the JSON column contains a child named 'schools' which is an array,
and the second element of the array has a child named 'type'.
Result:
ENO 'True'
------------------
1 True
Related Information:
Setting Up the ExistValue Examples
The following query results in 'True' if the JSON column contains a child named 'schools' which is an array,
and the second element of the array has a child named 'location'.
Result:
The JSON column contains a child named 'schools' which is an array, and the second element of the array
does not have a child named 'location'; therefore, the method did not return any rows.
Related Information:
Setting Up the ExistValue Examples
JSONExtract
Extracts data from a JSON instance. The desired data is specified in a JSONPath expression.
The result is a JSON array composed of the values found, or NULL if there are no matches.
JSONExtractValue
Allows you to retrieve the text representation of the value of an entity in a JSON instance,
specified using JSONPath syntax.
JSONExtractLargeValue
JSONExtractLargeValue is the same as JSONExtractValue, except for the return type
and size.
The following table shows the main differences between the 3 methods.
Can Extract
Method Return Value Return Type
Multiple Values
JSONExtract
The JSONExtract method operates on a JSON instance, to extract data identified by the JSONPath
formatted string. If one or more entities are found, the result of this method is a JSON array composed of
the values found; otherwise, NULL is returned.
JSONExtract searches the JSON object specified by JSON_expr and retrieves the data that matches the
entity name specified by JSONPath_expr.
Entity specified by JSONPath_expr found in JSON instance. JSON array whose elements are all the
matches for the JSONPath_expr in the
JSON instance
Related Information:
JSON String Syntax
JSONExtract Syntax
JSON_expr.JSONExtract (JSONPath_expr)
Syntax Elements
JSON_expr
An expression that evaluates to a JSON data type.
JSONPath_expr
An expression to extract information about a particular portion of a JSON instance. For
example, $.employees.info[*] provides all the information about each employee.
The desired information can be any portion of a JSON instance; for example, a name/value
pair, object, array, array element, or a value.
The JSONPath expression must be in JSONPath syntax.
JSONPath_expr cannot be NULL. If the expression is NULL, an error is reported.
JSONExtract Examples
The example uses the JSONExtract method to extract unfiltered results from a table. This example shows
multiple results being returned.
Note:
The example uses the table(s) created earlier.
ENO edata.JSONExtract(…)
--------------------------
1 [ "Cameron" ]
2 [ "Melissa" ]
3 [ "Alex" ]
4 [ "David" ]
Related Information:
Setting Up the JSONExtract Examples
The example uses the JSONExtract method to extract filtered results from a table.
Note:
The example uses the table(s) created earlier.
/* JSONExtract with filtered results - get the name of everyone older than
23. */
Result:
ENO edata.JSONExtract(…)
---------------------------
1 [ "Cameron" ]
2 ?
3 [ "Alex" ]
4 [ "David" ]
Related Information:
Setting Up the JSONExtract Examples
The example uses the JSONExtract method to extract NULL results from a table.
Note:
The example uses the table(s) created earlier.
Result: A NULL is returned for a person in the table who does not have a job.
ENO edata.JSONExtract(…)
--------------------------
1 [ "programmer" ]
2 ?
3 [ "CPA" ]
4 [ "small business owner" ]
Related Information:
Setting Up the JSONExtract Examples
More than one value matches query expression specified by Warning and error message string "***
JSONPath_expr. ERROR MULTI RESULT ***"
Related Information:
JSON String Syntax
Syntax Elements
JSON_expr
An expression that evaluates to a JSON data type.
JSONPath_expr
An expression to extract information about a particular portion of a JSON instance.
The retrieved data can be any portion of a JSON instance; for example, a name/value pair,
object, array, array element, or a value, as long as the returnable part is a scalar value such
as a string, number, boolean, or null.
The JSONPath expression must be in JSONPath syntax.
JSONPath_expr cannot be NULL. If the expression is NULL, an error is reported.
Result Types
JSONExtractValue returns a VARCHAR of the desired attribute. The returned length defaults to 4K, but can
be increased to 32000 characters (not bytes) using the DBS Control field JSON_AttributeSize. If the result
of the method is too large for the buffer, an error is reported.
JSONExtractLargeValue extracts a scalar value up to the maximum size of the type. It returns a
CLOB of 16776192 characters for CHARACTER SET LATIN or 8388096 characters for CHARACTER
SET UNICODE.
The return value of both methods can be UNICODE or LATIN, depending on the character set of the JSON
type that invoked it. If the parameter character set does not match the character set of the JSON type,
Vantage attempts to translate the parameter character set to the correct character set.
You can cast the return string to any data type that supports casting.
This example shows how to use the JSONExtractValue method to return multiple results. This query gets
the name of any school that has a type with a value like 'college'.
Note:
JSONExtractLargeValue can be substituted for JSONExtractValue.
Result:
ENO edata.JSONExtractValue(…)
-----------------------------
1 UCI
2 Mira Costa
3 CSUSM
4 ?
Related Information:
Setting Up the JSONExtractValue and JSONExtractLargeValue Examples
This example shows how to use the JSONExtractValue method to filter results. This query gets the name
of all persons older than 23.
Note:
JSONExtractLargeValue can be substituted for JSONExtractValue.
Result:
ENO edata.JSONExtractValue(…)
-----------------------------
1 Cameron
2 ?
3 Alex
4 David
Related Information:
Setting Up the JSONExtractValue and JSONExtractLargeValue Examples
This example shows the JSONExtractValue method returning NULL results. This query gets the job
associated with each person.
Note:
JSONExtractLargeValue can be substituted for JSONExtractValue.
Result:
ENO edata.JSONExtractValue(…)
---------------------------------
1 programmer
2 ?
3 CPA
4 small business owner
Related Information:
Setting Up the JSONExtractValue and JSONExtractLargeValue Examples
This example shows the warning and error message string returned when JSONExtractValue finds
multiple results to extract. The query attempts to get the name of every school for each person.
Note:
JSONExtractLargeValue can be substituted for JSONExtractValue.
Result:
eno edata.JSONEXTRACTVALUE('$..schools..name')
--------------------------------------------------
1 *** ERROR MULTI RESULT ***
2 *** ERROR MULTI RESULT ***
3 *** ERROR MULTI RESULT ***
4 *** ERROR MULTI RESULT ***
Related Information:
Setting Up the JSONExtractValue and JSONExtractLargeValue Examples
Result:
Result:
Related Information:
Setting Up the JSONExtractValue and JSONExtractLargeValue Examples
KEYCOUNT
A JSON method that returns the number of keys in a JSON document.
An integer representing the number of keys in the JSON document. Every valid query path is counted as a
key. This includes every value of a JSON ARRAY since the values can be accessed by the index.
KEYCOUNT Syntax
json_object.KEYCOUNT (depth)
Syntax Elements
depth
An optional clause that counts keys up to the specified depth.
The value is a positive integer that specifies the maximum depth for the search. The default
value is the maximum depth of the JSON instance.
KEYCOUNT Examples
Example
SELECT new
JSON('{"person":{"name":"will","occupation":"engineer","disposition":"silly"}}')
.keycount(1);
NEW JSON('{"person":{"name":"will","occupation":"engineer","disposition":"
---------------------------------------------------------------------------
1
Example
NEW JSON('{"name":"will","occupation":"engineer","disposition":"silly"}',
---------------------------------------------------------------------------
3
METADATA
A JSON method to return metadata on the document.
This method returns a JSON object with the following format:
{
“size”: Total uncompressed size of the document.
“depth”: Levels of nesting (starts at 1).
“keycnt”: Number of keys.
“objcnt”: Number of values that are Objects.
“arycnt”: Number of values that are Arrays.
“strcnt”: Number of values that are Strings.
“nbrcnt”: Number of values that are Numbers.
“boolcnt”: Number of values that are “true” or “false”.
“nullcnt”: Number of values that are “null”.
“keylen”: Minimum, maximum, and average number of characters in the keys.
}
METADATA Syntax
json_object.METADATA ()
METADATA Example
SELECT new JSON('{"name":"will","occupation":"engineer","disposition":"silly"}')
.metadata();
-------------------------------------------------------------------
{
"size":61
"depth":1
"keycnt":3
"objcnt":1
"arycnt":0
"strcnt":3
"nbrcnt":0
"boolcnt":0
"nullcnt":0
"keylen":
{
"min":4
"max":11
"avg":8
}
"strvallen":
{
"min":4
"max":8
"avg":6
}
}
StorageSize
The StorageSize method returns the number of bytes needed to store the input JSON data in the specified
storage format.
An INTEGER value representing the number of bytes required to store the input JSON data in the format
specified by the storage_format argument.
Note:
It is possible that the value returned by this method may change in the future because these storage
formats are not guaranteed to remain unchanged.
StorageSize Syntax
JSON_expr.StorageSize ( [ storage_format ] )
Syntax Elements
JSON_expr
An expression that evaluates to a JSON data type.
The JSON data can be stored in any of the supported storage formats.
storage_format
One of the following character strings, or an expression that evaluates to one of these
string values:
• 'LATIN_TEXT'
• 'UNICODE_TEXT'
• 'BSON'
• 'UBJSON'
These string values are not case sensitive.
If you do not specify a storage format, the default format used by the method is the storage
format of the input JSON data.
Usage Notes
This method is very helpful in estimating the benefits derived from a particular storage format for a specified
set of data. Because the JSON data need not be stored in a table to invoke this method, you can use it to
determine which storage format best suits a particular set of data before loading it into a table.
Result:
NEW JSON('{"hello":"world"}').StorageSize()
-------------------------------------------
17
Result:
NEW JSON('{"hello":"world"}').StorageSize('UNICODE_TEXT')
---------------------------------------------------------
34
Result:
NEW JSON('{"hello":"world"}').StorageSize('BSON')
-------------------------------------------------
22
Result:
NEW JSON('{"hello":"world"}').StorageSize('UBJSON')
---------------------------------------------------
23
ARRAY_TO_JSON
The ARRAY_TO_JSON function converts a Vantage ARRAY type to a JSON type composed of an ARRAY.
The ARRAY_TO_JSON function only converts Vantage ARRAY types to the JSON type stored as text.
The ARRAY data type is mapped to a JSON-formatted string composed of an array, which can also be a
multidimensional array. If the data type of the ARRAY is a numeric predefined type, the array element maps
to a numeric type in the JSON instance. For all other types, the value added to the JSON instance is the
transformed value of each element of the ARRAY, which is stored in the JSON instance as a string. Note,
the JSON array should have the same number of elements as the ARRAY type.
The return type of this function is JSON.
ARRAY_TO_JSON returns NULL if the ARRAY_expr argument is null.
Related Information:
Maximum Length of a JSON Instance
ARRAY_TO_JSON Syntax
{ [TD_SYSFNLIB.] ARRAY_TO_JSON (ARRAY_expr) |
Syntax Elements
returns_clause
STYLE column_expr
}
You can use the RETURNS clause to specify the maximum length and character set of the
JSON type.
If you do not specify a RETURNS clause, the return type defaults to JSON data type with
UNICODE character set and a return value length of 64000 bytes, which supports up to
32000 UNICODE characters.
TD_SYSFNLIB
The name of the database where the function is located.
ARRAY_expr
An expression that evaluates to an ARRAY data type.
ARRAY_expr specifies the array to be converted to the JSON type.
RETURNS data_type
Specifies that data_type is the return type of the function.
data_type can only be JSON.
integer
A positive integer value that specifies the maximum length in characters of the JSON type.
If you do not specify a maximum length, the default maximum length for the character set is
used. If specified, the length is subject to a minimum of two characters and cannot be greater
than the maximum of 16776192 LATIN characters or 8388096 UNICODE characters.
CHARACTER SET
The character set for the return value of the function, which can be LATIN or UNICODE.
If you do not specify a character set, the default character set for the user is used.
ARRAY_TO_JSON Examples
Note:
The user must have permission to CREATE TYPE. For example, GRANT UDTTYPE on sysudtlib to
<userID> WITH GRANT OPTION;
id a b
-------------------------------------------
1 (1,2,3,4,5) (1,2,3,4,5,6,7,8,9)
Use the ARRAY_TO_JSON function to convert an array to a JSON type composed of an ARRAY and
return the data in both LATIN and UNICODE character sets.
Result:
ARRAY_TO_JSON(a) ARRAY_TO_JSON(b)
----------------------------------------
[1,2,3,4,5] [[1,2,3], [4,5,6], [7,8,9]]
Related Information:
Setting Up the ARRAY_TO_JSON Examples
Aggregate the job positions into arrays containing the ages of the employees in those jobs.
Note:
The example uses the table(s) created earlier.
Result:
Related Information:
Setting Up the ARRAY_TO_JSON Using ARRAY_AGG Examples
Use the aggregated positions and ages from ARRAY_AGG as input to ARRAY_TO_JSON to obtain a
JSON object.
Note:
The example uses the table(s) created earlier.
Result:
pos ARRAY_TO_JSON(ARRAY_AGG(…))
---------------------------------------
engineer [24,34,25,21]
executive [50,51,52,52,60]
manager [40,41,45,48]
salesman [31,32,33,40]
Related Information:
Setting Up the ARRAY_TO_JSON Using ARRAY_AGG Examples
Note:
The example uses the table(s) created earlier.
Result:
Related Information:
Setting Up the ARRAY_TO_JSON Using ARRAY_AGG Examples
Use ARRAY_AGG to aggregate the salaries of the job positions as input to ARRAY_TO_JSON, to obtain
JSON objects as output.
Note:
The example uses the table(s) created earlier.
Result:
salary ARRAY_TO_JSON(ARRAY_AGG(…))
-----------------------------------
50000 ["engineer","engineer","salesman","salesman","manager"]
75000 ["engineer","salesman","manager"]
100000 ["engineer","salesman","manager"]
125000 ["manager","executive"]
150000 ["executive","executive"]
200000 ["executive"]
1000000 ["executive"]
Related Information:
Setting Up the ARRAY_TO_JSON Using ARRAY_AGG Examples
BSON_CHECK
The BSON_CHECK function checks a string for valid BSON syntax and provides an informative error
message about the cause of the syntax failure if the string is invalid.
Related Information:
JSON_CHECK
BSON_CHECK Syntax
[TD_SYSFNLIB.] BSON_CHECK ( BSON_data [, { 'STRICT' | 'LAX' } ] )
Syntax Elements
TD_SYSFNLIB
The name of the database where the function is located.
BSON_data
A string to be tested for compliance to BSON syntax.
BYTE, VARBYTE, and BLOB are the allowed input types.
A maximum of 16776192 bytes may be passed in for validation.
STRICT
The data is validated according to the BSON specification located at http://bsonspec.org/ and
the MongoDB restrictions.
The character string 'STRICT' is not case sensitive. For example, you can also specify 'strict'.
LAX
The data is validated according to the BSON specification located at http://bsonspec.org/.
This is the default behavior if you do not specify 'STRICT' or 'LAX'.
The character string 'LAX' is not case sensitive. For example, you can also specify 'lax'.
Usage Notes
This function only tests data for compliance with BSON syntax. It does not create a JSON instance.
You can use this function to validate JSON data that is in BSON format before loading it. This can save time
when loading a large amount of BSON data by preventing the rollback of an entire transaction in the event
of a BSON syntax error. The function also provides the necessary information to fix any syntax errors that
may be present before you load the data.
SELECT BSON_CHECK('160000000268656C6C6F0006000000776F726C640000'xb);
Result:
OK
SELECT BSON_CHECK('160000000268656C6C6F0005500000776F726C640000'xb);
Result:
DataSize
Returns the data length in bytes of any of the following Teradata variable maximum length complex
data types:
• DATASET
• JSON
• ST_Geometry
• XML
Returns a BIGINT that is the size in bytes of the data object passed in to the function.
DataSize Syntax
[TD_SYSFNLIB.] DataSize (var_max_length_cdt)
Syntax Elements
var_max_length_cdt
A DATASET, JSON, ST_Geometry, or XML data type object.
DataSize Examples
The following examples use JSON data types.
CREATE TABLE JSON_table (id INTEGER, jsn JSON INLINE LENGTH 1000);
id jsn
----------- ------------------------------------------
100 {"name" : "Mitzy", "age" : 3}
200 {"name" : "Rover", "age" : 5}
300 {"name" : "Princess", "age" : 4.5}
id datasize(jsn)
----------- --------------------
100 29
200 29
300 34
GeoJSONFromGeom
The GeoJSONFromGeom function converts an ST_Geometry object into a JSON document that conforms
to the GeoJSON standards.
Related Information:
Maximum Length of a JSON Instance
GeoJSONFromGeom Syntax
{ [TD_SYSFNLIB.] GeoJSONFromGeom ( geom_expr [, precision] ) |
Syntax Elements
returns_clause
STYLE column_expr
}
TD_SYSFNLIB
The name of the database where the function is located.
geom_expr
An expression which evaluates to an ST_Geometry object that represents a Point,
MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, or GeometryCollection.
precision
An integer specifying the maximum number of decimal places in the coordinate values.
If this argument is NULL or not specified, the default precision is 15.
RETURNS data_type
Specifies that data_type is the return type of the function.
data_type can be VARCHAR, CLOB, or JSON.
integer
Specifies the maximum length of the return type.
If you do not specify a maximum length, the default is the maximum length supported by the
return data type.
CHARACTER SET
The character set for the return value of the function, which can be LATIN or UNICODE.
If you do not specify a character set, the default character set for the user is used.
ANSI Compliance
This function is not compliant with the ANSI SQL:2011 standard.
Result Types
The result of this function is a JSON document which has a format that conforms to the GeoJSON
standards as specified in https://tools.ietf.org/html/rfc7946 .
If you do not specify a RETURNS clause, the default return type is JSON with UNICODE as the character
set. The length of the return value is 64000 bytes, which supports up to 32000 UNICODE characters. This
can result in a failure if the underlying ST_Geometry object exceeds 64000 bytes when converted to a
GeoJSON value.
Note:
The length of the converted GeoJSON value is greater than the underlying length of the
ST_Geometry object.
The usual rules apply for the maximum length of each data type.
UNICODE 32000
UNICODE 1048544000
UNICODE 8388096
Usage Notes
The GeoJSONFromGeom and GeomFromGeoJSON functions operate on or produce data in the JSON
text format. They cannot be used on JSON data that is stored in one of the binary formats. However, JSON
data stored in a binary format can be cast to/from its text representation to interact with these functions.
The casting can be done implicitly, so little effort is needed to perform this operation.
GeoJSONFromGeom Examples
These examples show the following conversions of ST_Geometry objects to JSON documents that
conform to GeoJSON standards:
• Conversion of Point ST_Geometry objects to JSON documents that have a data type of VARCHAR,
CLOB, or JSON.
• Conversion of various ST_Geometry objects to JSON documents that have a data type of
VARCHAR(2000) CHARACTER SET LATIN.
Result:
Result:
Result:
Result:
Result:
Result:
Result:
Result:
Result:
GeomFromGeoJSON
The GeomFromGeoJSON function converts a JSON document that conforms to the GeoJSON standards
into an ST_Geometry object.
An ST_Geometry object which contains the data that was stored in the JSON document.
GeomFromGeoJSON Syntax
[TD_SYSFNLIB.] GeomFromGeoJSON ( geojson_expr, asrid )
Syntax Elements
TD_SYSFNLIB
The name of the database where the function is located.
geojson_expr
An expression which evaluates to a JSON document conforming to the GeoJSON
standards as specified in https://tools.ietf.org/html/rfc7946 . The GeoJSON text string can
be represented as a VARCHAR, CLOB, or JSON instance in the LATIN or UNICODE
character set.
This GeoJSON text string must represent a geometry object. The value of the type member
must be one of these strings: "Point", "MultiPoint", "LineString", "MultiLineString", "Polygon",
"MultiPolygon", or "GeometryCollection".
asrid
An integer which specifies the Spatial Reference System (SRS) identifier assigned to the
returned ST_Geometry object.
ANSI Compliance
This function is not compliant with the ANSI SQL:2011 standard.
Usage Notes
The GeoJSONFromGeom and GeomFromGeoJSON functions operate on or produce data in the JSON
text format. They cannot be used on JSON data that is stored in one of the binary formats. However, JSON
data stored in a binary format can be cast to/from its text representation to interact with these functions.
The casting can be done implicitly, so little effort is needed to perform this operation.
GeomFromGeoJSON Examples
Examples: Valid GeoJSON Geometry Objects
The following show examples of valid GeoJSON geometry objects. The objects are valid based on the
GeoJSON format specification which you can access at: https://tools.ietf.org/html/rfc7946 .
Point
LineString
{ "type": "LineString",
"coordinates": [ [100.0, 0.0], [101.0, 1.0] ]
}
{ "type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
]
}
{ "type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ],
[ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ]
]
}
MultiPoint
{ "type": "MultiPoint",
"coordinates": [ [100.0, 0.0], [101.0, 1.0] ]
}
MultiLineString
{ "type": "MultiLineString",
"coordinates": [
[ [100.0, 0.0], [101.0, 1.0] ],
[ [102.0, 2.0], [103.0, 3.0] ]
]
}
MultiPolygon
{ "type": "MultiPolygon",
"coordinates": [
[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],
[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]],
[[ 100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]
]
}
GeometryCollection
{ "type": "GeometryCollection",
"geometries": [
{ "type": "Point",
"coordinates": [100.0, 0.0]
},
{ "type": "LineString",
"coordinates": [ [101.0, 0.0], [102.0, 1.0] ]
}
]
}
Result:
Result
------
POINT (100 0)
*** Failure 9134 GeomFromGeoJSON: conversion from JSON text failed, invalid
GeoJSON input.
Statement# 1, Info =0
JSON_CHECK
The JSON_CHECK function checks a string for valid JSON syntax and provides an informative error
message about the cause of the syntax failure if the string is invalid.
Related Information:
JSON String Syntax
BSON_CHECK
JSON_CHECK Syntax
[TD_SYSFNLIB.] JSON_CHECK ( '{string}' )
Note:
You must type the colored or bold braces.
Syntax Elements
TD_SYSFNLIB
The name of the database where the function is located.
'{string}'
The string to be tested for compliance to JSON syntax.
CHAR, VARCHAR, and CLOB are the allowed input types.
LATIN and UNICODE are allowed for all data types.
A maximum of 8388096 UNICODE characters or 16776192 LATIN characters may be
passed in for validation.
Usage Notes
This function only tests data for compliance with JSON syntax. It does not create a JSON instance.
You can use this function to validate JSON data before loading it. This can save time when loading a large
amount of JSON data by preventing the rollback of an entire transaction in the event of a JSON syntax error.
The function also provides the necessary information to fix any syntax errors that may be present before
you load the data.
Note:
You cannot use JSON_CHECK to validate JSON data that is stored in one of the binary formats. To
validate BSON data, use the BSON_CHECK function.
JSON_CHECK Examples
Valid JSON String Argument
The string passed to JSON_CHECK in this query has valid JSON syntax; therefore, the function
returns 'OK'.
JSONGETVALUE
Extracts a value from a JSON object as a specific type. The user specifies the data type and the function
returns a NULL if the conversion to that data type fails.
For more information about jsonPath, see JSONPath Request.
JSONGETVALUE Syntax
JSONGETVALUE ( jsonObject, jsonPath AS data_type )
Syntax Elements
jsonObject
Any JSON type object, including storage formats BSON and UBJSON.
jsonPath
The path to the value being extracted.
data_type
One of the following supported data types:
• BYTEINT
• SMALLINT
• INT
• BIGINT
• FLOAT
• DECIMAL
• NUMBER
• VARCHAR (LATIN or UNICODE)
• CHAR (LATIN or UNICODE)
• DATE
• TIME (with zone)
• TIMESTAMP (with zone)
• All INTERVAL types
JSONGETVALUE Example
SELECT JSONGETVALUE(new JSON('{"num":-2.5}'), '$.num' AS NUMBER);
JSON_KEYS
The JSON_KEYS table operator parses a JSON instance (represented as CHAR, VARCHAR, CLOB, or
JSON) and returns a list of key names.
JSON_KEYS Syntax
[TD_SYSFNLIB.] JSON_KEYS (
ON (json_expr)
[ USING name_value_pair [...] ]
) [AS] correlation_name
Syntax Elements
name_value_pair
TD_SYSFNLIB
The name of the database where the function is located.
json_expr
An expression that evaluates to correct JSON syntax. The expression can be a CHAR,
VARCHAR, CLOB, or JSON representation of a JSON data type in LATIN or UNICODE.
AS
An optional keyword introducing correlation_name.
correlation_name
An alias for the input column specified by the ON clause.
ANSI SQL refers to aliases as correlation names. They are also referred to as
range variables.
DEPTH
An optional clause that restricts the depth of the search. value is a positive integer that
specifies the maximum depth for the search.
The default value is the maximum depth of the JSON instance.
For example, setting the depth to 1 gets the top level keys in the JSON instance.
QUOTES
An optional clause that specifies whether or not the key names in the result are enclosed in
double quotation marks as follows:
• If value is 'Y' or 'y', the key names in the result are enclosed in double quotation marks.
This is the default if the USING QUOTES clause is not specified.
• If value is 'N' or 'n', the key names in the result are not enclosed in double
quotation marks.
Result Types
JSON_KEYS performs a search on the JSON instance to the specified depth and returns a VARCHAR
column of key names.
If a JSON array is present in the document, one result per index of each array is generated.
All results are given according to their "path" in the JSON document. Therefore, the output corresponding
to a nested key will contain all of the parent keys, in addition to itself.
If you specify the USING QUOTES ('Y') clause, the key names in the result set are enclosed in double
quotation marks. This is the default behavior and allows you to copy and paste a path into an SQL
statement, using the path as a dot notation entity reference on a JSON document, without any potential for
improper use of SQL keywords.
If you specify the USING QUOTES ('N') clause, the key names in the result set are not enclosed in double
quotation marks. This allows you to use the output as input to one of the JSON extraction methods, such
as JSONExtractValue.
JSON_KEYS returns an empty string when the input to the table operator is an empty JSON object.
JSON_KEYS Examples
"clouds": {
"all": 0
},
"dt": 1375292971,
"id": 2172797,
"name": "Cairns",
"cod": 200
}
');
},
"dt": 1375292971,
"id": 2172797,
"name": "Los Angeles",
"cod": 200
}
');
The example uses JSON_KEYS to obtain the key names of a JSON object. No depth is specified, so the
entire depth is searched and the keys at all levels are returned.
Result:
KEYS
----
“coord”
“coord”.”lon”
“coord”.”lat”
“sys”
“sys”.”country”
“sys”.”sunrise”
“sys”.”sunset”
“weather”
“weather”[0].”id”
“weather”[0].”main”
“weather”[0].”description”
“weather”[0].”icon”
“base”
“main”
“main”.”temp”
“main”.”humidity”
“main”.”pressure”
“main”.”temp_min”
“main”.”temp_max”
“wind”
“wind”.”speed”
“wind”.”deg”
“clouds”
“clouds”.”all”
“dt”
“id”
“name”
“cod”
Related Information:
Setting Up the JSON_Keys Examples
Example: Use JSON_Keys to Obtain Key Names for the Top Level Depth
The example uses JSON_KEYS to obtain the key names from the top level depth.
Result:
JSONKEYS
--------
“coord”
“sys”
“weather”
“base”
“main”
“wind”
“clouds”
“dt”
“id”
“name”
“cod”
Related Information:
Setting Up the JSON_Keys Examples
Result:
KEYS
----
"x"
"x"."a"
"x"."a"."b"
"y"
Result:
KEYS
----
x
x.a
x.a.b
y
Result:
KEYS
----
"x"
"x"."a"
"x"."a"."b"
"y"
Related Information:
Setting Up the JSON_Keys Examples
The example uses JSON_KEYS to get an ordered list of all unique keys from all documents of all rows of
a table.
Result:
JSONKeys
--------
"base"
"clouds"
"clouds"."all"
"cod"
"coord"
"coord"."lat"
"coord"."lon"
"dt"
"id"
"main"
"main"."humidity"
"main"."pressure"
"main"."temp"
"main"."temp_max"
"main"."temp_min"
"main"."temp_scale"
"name"
"sys"
"sys"."country"
"sys"."sunrise"
"sys"."sunset"
"weather"
"weather"[0]
"weather"[0]."description"
"weather"[0]."icon"
"weather"[0]."id"
"weather"[0]."main"
"wind"
"wind"."deg"
"wind"."speed"
Related Information:
Setting Up the JSON_Keys Examples
The example uses JSON_KEYS to get an ordered list of all unique keys from all documents of all rows of
a table. The key names returned in the result are not enclosed in double quotation marks.
Result:
JSONKeys
--------
base
clouds
clouds.all
cod
coord
coord.lat
coord.lon
dt
id
main
main.humidity
main.pressure
main.temp
main.temp_max
main.temp_min
main.temp_scale
name
sys
sys.country
sys.sunrise
sys.sunset
weather
weather[0]
weather[0].description
weather[0].icon
weather[0].id
weather[0].main
wind
wind.deg
wind.speed
Related Information:
Setting Up the JSON_Keys Examples
The example uses JSON_KEYS with JSONExtractValue to extract all values of the JSON document.
Result:
JSONKeys json_data.JSONEXTRACTVALUE(('$.'||JSONKeys))
---------------------------------------------------------------------------
base global stations
clouds {"all":0}
clouds.all 0
cod 200
coord {"lon":145.766663,"lat":-16.91667}
coord.lat -16.91667
coord.lon 145.766663
dt 1375292971
id 2172797
main
{"temp":288.97,"humidity":99,"pressure":1007,"temp_min":288.71,"temp_max":289.15
}
main.humidity 99
main.pressure 1007
main.temp 288.97
main.temp_max 289.15
main.temp_min 288.71
name Cairns
sys
{"country":"AU","sunrise":1375216946,"sunset":1375257851}
sys.country AU
sys.sunrise 1375216946
sys.sunset 1375257851
weather [{"id":800,"main":"Clear","description":"Sky is
Clear","icon":"01n"}]
weather[0] {"id":800,"main":"Clear","description":"Sky is
Clear","icon":"01n"}
weather[0].description Sky is Clear
weather[0].icon 01n
weather[0].id 800
weather[0].main Clear
wind {"speed":5.35,"deg":145.001}
wind.deg 145.001
wind.speed 5.35
Result:
JSONKeys json_data.JSONEXTRACTVALUE(('$.'||JSONKeys))
---------------------------------------------------------------------------
base global stations
clouds {"all":0}
clouds.all 0
cod 200
coord {"lon":245.766663,"lat":-16.91667}
coord.lat -16.91667
coord.lon 245.766663
dt 1375292971
id 2172797
main
{"temp":288.97,"humidity":99,"pressure":1007,"temp_min":288.71,"temp_max":289.15
,"temp_scale":"Fahrenheit"}
main.humidity 99
main.pressure 1007
main.temp 288.97
main.temp_max 289.15
main.temp_min 288.71
main.temp_scale Fahrenheit
name Los Angeles
sys
{"country":"US","sunrise":1375216946,"sunset":1375257851}
sys.country US
sys.sunrise 1375216946
sys.sunset 1375257851
weather [{"id":800,"main":"Clear","description":"Sky is
Cloudy","icon":"01n"}]
weather[0] {"id":800,"main":"Clear","description":"Sky is
Cloudy","icon":"01n"}
weather[0].description Sky is Cloudy
weather[0].icon 01n
weather[0].id 800
weather[0].main Clear
wind {"speed":5.35,"deg":145.001}
wind.deg 145.001
wind.speed 5.35
Related Information:
Setting Up the JSON_Keys Examples
JSONMETADATA
An aggregate function that returns metadata about a set of JSON documents.
This method returns a JSON object with the following format:
{
"doc_count":
“size”:
{
“min”:
“max”:
“avg”:
}
“depth”:
{
“min”:
“max”:
“avg”:
}
“keycnt”:
{
“min”:
“max”:
“avg”:
}
“objcnt”:
{
“min”:
“max”:
“avg”:
}
“arycnt”:
{
“min”:
“max”:
“avg”:
}
“strcnt”:
{
“min”:
“max”:
“avg”:
}
“nbrcnt”:
{
“min”:
“max”:
“avg”:
}
“boolcnt”:
{
“min”:
“max”:
“avg”:
}
“nullcnt”:
{
“min”:
“max”:
“avg”:
}
"keylen":
{
"min":
"max":
"avg":
}
"strvallen":
{
"min":
"max":
"avg":
}
}
For each field, a minimum, maximum, and average is calculated across the entire input set of JSON
documents. doc_count is the total number of rows which were counted by the aggregation.
JSONMETADATA Syntax
JSONMETADATA (jsonObject)
Syntax Elements
jsonObject
Any JSON type object, including storage formats BSON and UBJSON.
NVP2JSON
A function to convert a string of name-value pairs to a JSON object.
A JSON object with one level of depth containing all the key/value pairs found in the NVP_string. If invalid
data is discovered while parsing the input, a NULL is returned.
NVP2JSON Syntax
[TD_SYSFNLIB.] NVP2JSON (
NVP_string
[, name_delimiters, value_delimiters [, ignore_characters] ]
)
Syntax Elements
NVP_string
A string in the name/value pair format.
name_delimiters
Optional.
One or more delimiters of one or more character each. This is the delimiter used between
separate name/value pairs.
value_delimiters
Optional.
One or more delimiters of one or more characters each. The delimiter is used between names
and corresponding values.
ignore_characters
Optional.
One or more characters that are removed from the beginning of Names of the NVP as the
names are converted to JSON keys.
For more information on the usage of the name_delimiters and value_delimiters, see
Teradata Vantage™ - SQL Functions, Expressions, and Predicates, B035-1145. This
function has the same behavior for those arguments.
The expressions passed to this function must have the following data types:
• NVP_string = CHAR, VARCHAR, CLOB : LATIN or UNICODE
• name_delimiters = CHAR, VARCHAR, CLOB : LATIN or UNICODE
• value_delimiters = CHAR, VARCHAR, CLOB : LATIN or UNICODE
• ignore_characters = CHAR, VARCHAR, CLOB : LATIN or UNICODE
Usage Notes
If any of the parameters are NULL or an empty string, the result is NULL.
If the size of the NVP_string is greater than 16 MB, then the result is NULL.
The function tries to return an appropriately-sized JSON result object with its character set the same as the
input NVP_string’s character set. If the result is too large to fit in the JSON result type, a NULL is returned.
You may use the RETURNS clause with this function to specify the size of the JSON object to return. If the
result does not fit in the specified JSON object, then a NULL is returned.
The only supported types for the RETURNS clause are JSON(*) CHARACTER SET LATIN and JSON(*)
CHARACTER SET UNICODE where * can be any valid size for JSON.
If the input parameters consist of mixed character types, then all the input parameter character types
are converted to the RETURNS clause character type if it is specified, or converted to the NVP_string
character type.
NVP2JSON Examples
Example
NVP2JSON('name=will&occupation=engineer&hair=blonde')
---------------------------------------------------------------------------
{"name":"will","occupation":"engineer","hair":"blonde"}
Example
NVP2JSON('name->will+occupation->engineer+hair->blonde','+','->')
---------------------------------------------------------------------------
{"name":"will","occupation":"engineer","hair":"blonde"}
Example
NVP2JSON('_name=will&occupation=engineer&!hair=blonde','&','=','!_')
---------------------------------------------------------------------------
{"name":"will","occupation":"engineer","hair":"blonde"}
JSON_TABLE
This table operator shreds some or all of the data in a JSON document, and creates a derived
database table based on the shredded data. TD_JSONSHRED is recommended for faster
shredding of large JSON documents (those with large numbers of attributes or that will shred
to many database records).
TD_JSONSHRED
This table operator is similar to JSON_TABLE. It shreds some or all of the data in a JSON
document, and creates a derived database table based on the shredded data. It accepts larger
JSON documents and shreds the data significantly more quickly, but it does not support the
use of JSONPath expressions, and supports fewer output data types.
The table operators and stored procedures operate on JSON documents stored in text or binary format.
Pass-through columns Appear after shredded JSON Appear before shredded JSON data in the
(columns that do not data in returned table returned table
contain JSON data in the
input table)
Handling of oversized Truncates shredded JSON Truncates shredded JSON data that is
shredded string data data that is longer than the longer than the RETURNTYPE data size
data "type" specification in the specification. Optionally, you can have
COLEXPR parameter TD_JSONSHRED fail with an error if
the shredded data exceeds the type
specification. The error indicates the
problematic column.
{"pkey":123,"val":1234}
You can use the following INSERT statement to shred this data into the MyTable table:
You can also use the JSON_TABLE table operator to shred this data, but the SQL is more complicated than
using the INSERT statement.
Another advantage of using the INSERT statement is that this operation is a one-AMP operation when
shredding JSON data that is a string literal. This can result in a performance improvement.
Note:
For parameterized SQL, INSERT...JSON supports VARCHAR, CLOB, and external JSON data types;
however, the operation is handled as a two-AMP process. For improved performance, it is better to
directly specify a JSON literal.
pkey INTEGER,
val INTEGER,
jsn JSON CHARACTER SET LATIN AUTO COLUMN
);
You can also use the ALTER TABLE statement to add a JSON AUTO COLUMN to a table or to convert an
existing JSON column into an AUTO COLUMN.
During shredding, when the JSON root object contains an attribute that does not match an existing column,
the extra attributes are aggregated and inserted into the JSON AUTO COLUMN. If the table does not have
a JSON AUTO COLUMN, the extra attributes are ignored.
For example, suppose you issue the following INSERT statement:
The jsn column will contain the following JSON data which did not match any of the columns in MyTable:
{"extra":"1234"}
Be aware that if you compose the shredded JSON data back into JSON, the data may not match the original
JSON. For example, if you use SELECT AS JSON to compose the JSON data that was shredded in the
previous INSERT statement:
{"pkey":123,"val":1234,"jsn":{"extra":"1234"}}
a INTEGER,
b INTEGER,
c INTEGER NOT NULL DEFAULT 5,
j JSON CHARACTER SET LATIN
);
In the following INSERT statement, the input JSON data does not include a value for column c. However,
because the input JSON data is a string literal, the DEFAULT value defined for column c is inserted.
In this example, the input JSON data includes extra data that does not match any columns in jsonTable5.
The extra data is inserted into the j AUTO COLUMN. The third INSERT statement does not include any
extra data and the AUTO COLUMN is defined as NOT NULL; therefore, '{}' is inserted.
a b j
----------- ----------- ---------------------------------------------------
1 1 {"extra":"1"}
2 2 {"extra1":"2","extra2":"222"}
3 3 {}
JSON_TABLE
JSON_TABLE creates a derived table based on the contents of a JSON document.
JSON_TABLE takes a JSON instance and creates a derived 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.
JSON_TABLE Syntax
[TD_SYSFNLIB.]JSON_TABLE(
ON (json_documents_retrieving_expr)
USING
ROWEXPR (row_expr_literal)
COLEXPR (column_expr_literal)
[AS] correlation_name [(column_name [,...])]
)
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.
The column containing the JSON document can be of any of these data types: CHAR,
VARCHAR, CLOB, BYTE, VARBYTE, or BLOB.
This is an example of the json_documents_retrieving_expr.
row_expr_literal
A JSONPath query expression that returns an array of objects from the JSON document
returned by json_documents_retrieving_expr. JSON_TABLE returns one row for each object
in the array.
row_expr_literal cannot be NULL.
column_expr_literal
A JSONPath query expression that identifies individual object in the JSON array returned by
row_expr_literal. Each object will be represented by a column in the returned table.
Note:
These name:value pairs are case-sensitive.
• "jsonpath" : JSONPath_query_expression
The query that extracts the value of this column from the JSON document resulting from
the row_expr_literal expression.
Alternatively, the value of the column can also be extracted from the root of the original
JSON document if you use the fromRoot : true name-value pair described below.
• "type" : data_type_of_output_table_column
Specifies the Vantage data type that will be assigned to the output column. It must be
specified for every column of shredded JSON data in the returned table.
The data type of the columns of the output table may be any non-LOB predefined
Vantage type. JSON_TABLE does not support UDTs and LOB types in the output, so the
Vantage JSON data type itself cannot be a type assigned to any of the JSON data output
columns. For a list of the supported data types, see Supported Output Data Types below.
• "ordinal" : true
An optional attribute that indicates that the column in the returned table will be an ordinal
column. Each row in 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.
• "fromRoot" : true
An optional attribute that indicates the JSONPath query is evaluated with the object
returned by json_documents_retrieving_expression, as the root object. Otherwise the
JSONPath query is evaluated using the object returned by row_expr_literal as the
root object.
This allows you to include data from the original JSON document in the output table in
addition to data from the array returned by row_expr_literal.
AS
Optional keyword introducing correlation_name.
correlation_name
An alias for the table that is referenced by json_documents_retrieving_expr.
ANSI SQL refers to table aliases as correlation names. They are also referred to as
range variables.
column_name
An optional list of one or more column names.
Result Types
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 rows returned by JSON_TABLE have the following columns:
• 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.
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.
Note:
When a JSON value is shredded to populate a CHAR, VARCHAR, or VARBYTE column, if the size
of the value is larger than the size of the target column, the value is truncated to fit the column.
The following lists the supported data types for JSON_TABLE output. The square brackets indicate
optional values in the type specification.
CHAR(n)
VARCHAR(n)
BYTE(n)
VARBYTE(n)
BYTEINT
SMALLINT
INTEGER
FLOAT/REAL
DECIMAL/NUMERIC [(n,[m])]
NUMBER
DATE
TIME [(fractional_seconds_precision)]
TIMESTAMP [(fractional_seconds_precision)]
INTERVAL DAY[(precision)]
JSON_TABLE Examples
Note:
The example uses the table(s) created earlier.
"type" : "CHAR(20)"},
{"jsonpath" : "$.type",
"type" : "VARCHAR(20)"}]')
) AS JT(id, schoolName, "type");
Result:
id schoolName type
------------------------------
1 Lake elementary
1 Madison middle
1 Rancho high
1 UCI college
Note:
The example uses the table(s) created earlier.
Result:
Related Information:
Setting Up the JSON_TABLE Examples
The example shows JSON_TABLE using extra columns, in addition to the required columns of ID and
JSON document. The column_expression_literal parameter requires a mapping of the columns in the
row_expression_literal to the columns of the output table of this function, as this example demonstrates.
To simplify this example, constants are used for the state and nation columns.
Note:
The example uses the table(s) created earlier.
Result:
Related Information:
Setting Up the JSON_TABLE Examples
Note:
The example uses the table(s) created earlier.
Result:
Related Information:
Setting Up the JSON_TABLE Examples
TD_JSONSHRED
TD_JSONSHRED shreds a JSON document and creates a derived database table containing the values
from the JSON data.
TD_JSONSHRED Syntax
[TD_SYSFNLIB.]TD_JSONSHRED(
ON (json_document_retrieving_expr)
USING
ROWEXPR(row_expr_literal)
COLEXPR(column_expr_literal [,...])
RETURNTYPES(return_type [,...])
[NOCASE(nocase_value)]
[TRUNCATE(truncate_value)]
) [AS] correlation_name [(column_name [,...])]
Syntax Elements
json_documents_retrieving_expr
A query expression that returns at least two columns:
• The first column serves as an identifier for the row and a way to correlate the rows in
the returned table to the data in a particular input JSON document. It also serves as the
primary index for the returned table, and can be any non-LOB data type. Best practice
is to have it be a unique value for each input row, which corresponds to each individual
JSON document to be processed by the function.
This column is returned unchanged (passed through) by TD_JSONSHRED as the first
column in the result set.
• The last column contains the JSON document to be shredded.
It can have data type JSON, CLOB, or VARCHAR. CLOB allows for a JSON document
input as large as 2GB.
The json_documents_retrieving_expr can return additional columns between the first id
column and the last JSON document column. These additional columns are passed through
by the function, and returned by TD_JSONSHRED unaltered from their input values. They
appear in the returned table before the columns of shredded JSON data.
If json_documents_retrieving_expr returns no results or NULL, TD_JSONSHRED returns a
table with no rows.
row_expr_literal
The identifier of the JSON object that contains the data to be shredded. TD_JSONSHRED
shreds the contents of the identified object into rows of the returned (output) table. If multiple
shredded objects have the same identifier, their contents are returned in different rows of
returned table.
An empty string signifies that TD_JSONSHRED should shred the entire contents of the input
JSON documents.
Note:
row_expr_literal does not support full JSONPath notation to locate and identify JSON
objects within the hierarchical structure of the input JSON document. Instead, you can
use a simplified dot notation.
Unlike JSONPath, this simplified dot notation does not use a $ to represent an
outer-level object, and it does not support wildcards or expressions. It identifies objects
and outer-level arrays within the JSON document. Simple dot notation can identify
nested JSON objects, but it cannot identify specific objects within an array.
column_expr_literal
The identifier of a JSON object within the shredded data. The value of the object becomes the
value of a column in the returned table. By default, the object identifier is used as the column
name. If multiple objects in the shredded data have the same identifier, their values become
the column values in multiple rows of the output table.
An empty string means all objects in the shredded data are returned in a single column.
Note:
column_expr_literal does not support full JSONPath notation to locate and identify
JSON objects within the hierarchical structure of the input JSON document. Instead,
you can use a simplified dot notation.
Unlike JSONPath, this simplified dot notation does not use a $ to represent an
outer-level object, and it does not support wildcards or expressions. It identifies objects
and outer-level arrays within the JSON document. Simple dot notation can identify
nested JSON objects, but it cannot identify specific objects within an array.
return_type
The data type assigned to a column of shredded JSON data in the returned derived table.
• The number of return types must correspond to the number of columns specified for
COLEXPR. (This does not include any pass-through columns of non-JSON data in the
original input table.)
• The output type must be enclosed in single quotation marks (apostrophes).
• Valid output data types are DECIMAL/NUMERIC, BYTEINT, SMALLINT, INTEGER,
BIGINT, REAL, FLOAT, DOUBLE PRECISION, DATE, VARCHAR, and CLOB.
Note:
The maximum string size for shredded JSON data is 16 MB.
nocase_value
Determines whether string matching for COLEXPR and ROWEXPR is case sensitive. The
value can be 0 or 1:
• 0 means string matching is case sensitive. This is the default.
• 1 means string matching is not case sensitive.
truncate_value
Determines how TD_JSONSHRED handles shredded data that exceeds the corresponding
RETURNTYPE specification. The value can be 0 or 1:
• 0 means TD_JSONSHRED fails with an error. The error indicates the
problematic column.
• 1 means TD_JSONSHRED returns data that is truncated to match the RETURNTYPE
specification. This is the default.
correlation_name
A name used as an alias for the derived table returned by TD_JSONSHRED.
column_name
Name for a column in the derived table. If you do not specify a column name,
TD_JSONSHRED uses the JSON object names. The number of column names must
match the number of columns in the derived table.
Note:
If more than one output column comes from JSON objects with identical names, you
must use the column_name parameter to assign different names to the output columns.
Result Types
TD_JSONSHRED returns a derived table with rows that have at least two columns:
• The first column is the indentifier column that was retrieved by the
json_documents_retrieving_expression. It is passed through, unchanged, from the results of that
retrieving expression query.
• The last columns returned are from the shredded JSON data, and must conform to the format and data
types defined by COLEXPR and RETURNTYPES.
• If the original json_documents_retrieving_expr returned additional columns between the ID and JSON
data columns, these pass-through columns are returned unmodified between the ID column and the
columns of shredded JSON data.
Usage Notes
• TD_JSONSHRED supports input CLOB data type sizes up to 2 GB.
• Although TD_JSONSHRED does not support extracting data directly from nested arrays in a JSON
document, you can nest calls to TD_JSONSHRED to shred data from nested arrays. For example:
The inner TD_JSONSHRED creates a derived table (d1) having a column that holds the value of the
JSON address object. This value is an array that was nested in the "employees" array in the original
JSON document. The nested address array itself is a JSON document, so its constituent values
can be shredded by the outer TD_JSONSHRED into individual columns in the derived table, d2. See
Example: TD_JSONSHRED Nested Arrays.
TD_JSONSHRED Examples
Example: TD_JSONSHRED
This example shreds data from two small JSON documents that have been inserted into a database
table, then displays the extracted data in columns of the derived table that is returned by the function.
The JSON documents are stored as VARCHAR data, but one column of the extracted data is returned as
DECIMAL data.
Now use TD_JSONSHRED to extract the names and values from the JSON data into a derived
database table:
In the returned result set below, notice the ID column values identify the row of the input table (and
therefore the specific JSON document) from which the data was shredded.
ID name price
----------- -------------------- ------------
1 charger 12.99
1 phone 599.99
2 laptop 999.99
This example shreds data from four small JSON documents that have been inserted into a database table.
The data that is in nested JSON object or arrays is extracted using nested calls to TD_JSONSHRED.
The JSON documents are stored as VARCHAR data, but the extracted price data is converted to a
decimal type.
Now use TD_JSONSHRED to extract the name and price data into a derived database table:
In the returned result set below, notice the ID column values identify the row of the input table (and therefore the specific JSON document) from
which the data was shredded.
ID name prices
----------- ---------- --------------------------------------------------------------------------------
3 cup [{"date":"2004-01-01","price":5.99},{"date":"2005-01-01","price":6.99}]
4 coffee {"date":"2001-01-01","price":1.99}
1 charger [{"date":"2001-01-01","price":12.99},{"date":"2002-01-01","price":13.99}]
1 phone {"date":"2001-01-01","price":12.99}
2 laptop [{"date":"2003-01-01","price":12.99},{"date":"2004-01-01","price":13.99}]
To shred the price data, we need to further shred the JSON objects in the price column. Because some of
the price data is in arrays, we cannot use dot notation to specify the data we want. Instead, we can access
the nested array and nested object data using nested calls to TD_JSONSHRED:
This example creates a small JSON document with nested objects, inserts it into a database table as
a CLOB data type, then uses TD_JSONSHRED and simplified dot notation to extract the data from the
unnested and nested objects. The JSON document is created as a CLOB object, but the extracted data
is returned as VARCHAR data.
In the returned result set below, notice that the data extracted and displayed as column c contains all the objects that are nested within JSON
object "items" : "c". Those nested objects are further broken out and displayed individually in columns c-d, c-e, c-e-y, and c-f by using dot
notation (c.c-d, c.c-e, c.c-e-y, and c.c-f) in the COLEXPR to identify the nested objects. The final JSON object in the document is empty, so the
last row in the derived table does not include values for the JSON data columns.
Although the original JSON document was stored as a CLOB, the data is shredded to VARCHAR columns.
This example creates a small JSON document with nested arrays, and inserts it into a database table as
a Vantage JSON data type. First we use TD_JSONSHRED to extract data from the outer array, then we
use nested TD_JSONSHRED calls to extract data from the inner arrays. Finally, we'll show how you can
extract data from different levels of arrays.
{
"sname":"Sloat",
"stype":"elementary"
},
{
"sname":"Aptos",
"stype":"middle"
},
{
"sname":"Lowell",
"stype":"high"
},
{
"sname":"UCB",
"stype":"college"
}
],
"job":"manager"
}
]
}');
The following single TD_JSONSHRED call retrieves only the "schools" entries from the "students" array, "students". Each row of the resulting
table contains the entire "schools" array for each student.
id schools
-- --------------------------------------------------------------------------------------
1 [{"sname":"Lake","stype":"elementary"},{"sname":"Madison","stype":"middle"},
{"sname":"Rancho","stype":"high"},{"sname":"UCI","stype":"college"}]
1 [{"sname":"Sloat","stype":"elementary"},{"sname":"Aptos","stype":"middle"},
{"sname":"Lowell","stype":"high"},{"sname":"UCB","stype":"college"}]
The following nested calls to TD_JSONSHRED allow you to get to the individual JSON object data within the "schools" arrays. The inner function
call is identical to the one above. Notice that, for each row in the output above, the schools column contains a valid JSON document. So you can
use that derived table as input to another call to TD_JSONSHRED.
RETURNTYPES('VARCHAR(150)')
) AS d1
)
USING
ROWEXPR('')
COLEXPR('sname', 'stype')
RETURNTYPES('VARCHAR(25)', 'VARCHAR(25)')
) AS d2;
id sname stype
----------- ------------------------- -------------------------
1 Lake elementary
1 Madison middle
1 Rancho high
1 UCI college
1 Sloat elementary
1 Aptos middle
1 Lowell high
1 UCB college
To include a column showing the student name associated with each school, include the "name" objects from the JSON document in the inner
call to TD_JSONSHRED. This example also aliases the column names in the outermost SELECT statement.
ROWEXPR('students')
COLEXPR('name','schools')
RETURNTYPES('VARCHAR(15)','VARCHAR(150)')
) AS d1
)
USING
ROWEXPR('')
COLEXPR('sname', 'stype')
RETURNTYPES('VARCHAR(25)', 'VARCHAR(25)')
) AS d2
;
These examples demonstrate two approaches that can be used to shred JSON data with multiple
potential row expressions.
Populate the table with sample JSON data, which will be shredded. Extract a and b under rowData1
and rowData2.
For the next insertion, note that the same id 1 is intentionally used here to illustrate the sequence
of events.
]
}');
SELECT a, b
FROM TD_JSONSHRED (ON
(SELECT id, data.JsonExtract('$.[rowData1,rowData2][*]')
FROM jsonTable)
USING
ROWEXPR('')
COLEXPR('a', 'b')
RETURNTYPES ('INTEGER', 'VARCHAR(10)')
) d(id, a, b)
;
Output:
a b
----------- ----------
1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 h
9 i
10 j
11 k
12 l
◦ The input data does not have a size restriction. However, it takes multiple rounds of shredding
the data, which has performance implications.
SELECT a, b
FROM TD_JSONSHRED (ON
(SELECT id, data
FROM jsonTable)
USING
ROWEXPR('rowData1')
COLEXPR('a', 'b')
RETURNTYPES ('INTEGER', 'VARCHAR(10)')
) d(id, a, b)
UNION ALL
SELECT a, b
FROM TD_JSONSHRED (ON
(SELECT id, data
FROM jsonTable)
USING
ROWEXPR('rowData2')
COLEXPR('a', 'b')
RETURNTYPES ('INTEGER', 'VARCHAR(10)')
) d2(id, a, b)
;
Output:
a b
----------- ----------
1 a
2 b
3 c
7 g
8 h
9 i
4 d
5 e
6 f
10 j
11 k
12 l
Notice that the output values sequence are different in each approach. In the first query using JsonExtract,
the row data is prepared once. In the second query using UNION ALL, the same data is processed and
shredded twice.
Populate the table with sample JSON data that will be shredded. Extract a and b under rowData as the
common data globalId.
SELECT globalId, a, b
FROM TD_JSONSHRED (ON
(SELECT id, CAST(data.globalId AS INTEGER), data FROM jsonTable)
USING
ROWEXPR('rowData')
COLEXPR('a', 'b')
RETURNTYPES ('INTEGER', 'VARCHAR(10)')
) d(id, globalId, a, b)
;
Output:
globalId a b
----------- ----------- ----------
1234 1 a
1234 2 b
1234 3 c
Shred operation fails. Nonzero value indicating specific error condition and appropriate
error message
Required Privileges
JSON_SHRED_BATCH resides in the SYSLIB database. The user executing the JSON_SHRED_BATCH
procedure must have the following privileges:
• EXECUTE PROCEDURE on SYSLIB
• SELECT privilege on the source table
• GRANT ALL (insert/update/delete/upsert) on the target table
The database where the procedure is executing must have all privileges on SYSUDTLIB, SYSLIB, and the
database where the target table exists and EXECUTE PROCEDURE on SYSLIB.
SYSLIB must have all privileges on the database which is executing the procedure.
For example, if the database where the procedure is executing and where the target table exists is called
JSONShred, then the following statements will assign the required privileges:
Note:
The following three privileges from above are mandatory:
• GRANT ALL ON SYSLIB TO JSONShred;
• GRANT EXECUTE PROCEDURE ON SYSLIB TO JSONShred;
• GRANT ALL ON JSONShred TO JSONShred;
If the login user is jsonshred and the target table is targetDB, then the following privileges are required:
If the source table, sourceDB, is different from targetDB, then the following privilege is also required:
Note:
You must type the colored or bold brackets.
Syntax Elements
shred_statement
{ row_expression,
column_expression,
[ query_expression, ]
table_expression
}
Note:
You must type the colored or bold braces.
row_expression
"rowexpr" : "JSONPath_expr"
column_expression
"colexpr" : [
{ "temp_column_name" : "JSONPath_expr" ,
"type" : "data_type"
[, "fromRoot" : true ]
} [,...]
Note:
You must type the colored or bold brackets and braces.
query_expression
"queryexpr" : [
Note:
You must type the colored or bold brackets and braces.
table_expression
"tables" : [
{ "table_name" : { metadata , column_assignment } } [,...]
Note:
You must type the colored or bold brackets and braces.
metadata
"metadata" : {
"operation" : { "insert" | "update" | "merge" | "delete" }
[, "keys" : [ "table_column_name" [,...] ] ]
[, "filter" : "filter_expression" ]
}
Note:
You must type the colored or bold brackets and braces.
column_assignment
"columns" : {
"table_column_name" : {
"temp_column_name" |
"temp_expr" |
numeric_constant |
["string_constant"] |
boolean_constant |
null
}
Note:
You must type the colored or bold brackets and braces.
CALL JSON_SHRED_BATCH
The following describes the parameters used by the JSON_SHRED_BATCH and
JSON_SHRED_BATCH_U procedure calls.
input_query
A string input parameter which specifies a query that results in a group of JSON instances
from which the user can perform shredding. Extra columns can result and be referred to in
the shred_statement.
If this parameter is NULL, an error is reported.
The input_query parameter can operate on one or more JSON objects in a source table. The
user invoking JSON_SHRED_BATCH must have SELECT privilege on the source table. The
input query is mapped to a JSON_TABLE function call. Since JSON_TABLE requires that the
first two columns specified be an ID value and a JSON object, respectively, the input_query
parameter also requires the first two columns to be an ID value and a JSON object.
The following are examples of an input_query string.
shred_statement
The shred statement element defines the mapping of the JSON instance, that resulted from
the input query, to where the data will be loaded in the user tables.
If the shred statement is NULL an error is reported.
All keywords in the shred statement must be specified in lowercase.
The following sections discuss the structure and syntax of the shred statement. Multiple
shred statements can be run, but there are performance impacts.
result_code
An output parameter representing the result of the shred operation. A value of 0 indicates
success. All non-zero values indicate specific error conditions and an appropriate error
message is returned.
row_expression
The following describes the variables used by the row expression.
• "rowexpr" :
Required, literal entry.
Must be lowercase.
• JSONPath expr – An expression in JSONPath syntax to extract information about a
particular portion of a JSON instance. For example, $.schools[*] identifies all schools.
column_expression
The following describes the column expression variables.
• "colexpr" :
Required, literal entry.
Must be lowercase.
• temp_column_name
Any user-defined name for the temporary column. The temporary column name must be
unique or an error is reported.
Note:
The names are not case sensitive. For example, col1 and COL1 will fail because
they are used in internal queries and are not unique.
Note:
Attempting to set fromRoot to false raises an error.
The user is responsible for mapping elements of the column_expression to acceptable data
types. No explicit casting is needed; the data is implicitly cast to the desired data type.
However, if the data does not correctly cast to the desired data type an error is reported,
therefore, care should be taken when determining the elements and data types of the
column_expression. If the result of the expression is an array or object (instead of a single
value), the only acceptable data types are CHAR or VARCHAR of suitable length.
The data type of the temporary columns in the output table of JSON_TABLE must be
specified. This is enforced with JSON_SHRED_BATCH and JSON_SHRED_BATCH _U in
the column_expression. It is necessary for the user to provide this information so that the data
may be correctly interpreted and used with the target table(s).
query_expression
The following describes the query expression variables.
• "queryexpr" :
Required, literal entry.
Must be lowercase.
• temp_column_name
Any user-defined name for the temporary column. The temporary column name must be
unique or an error is reported.
Note:
The names are not case sensitive. For example, col1 and COL1 will fail because
they are used in internal queries and are not unique.
Note:
The order of the columns is important while the names of the columns are not.
The data types in the queryexpr should match the actual data type of the columns specified in
the input_query. No explicit cast is added, so the data must be implicitly castable to the data
type defined in the queryexpr, if not the exact data type. Any errors encountered will result in
a failed shred, and are reported to the user.
The following example demonstrates the importance of the ordering. Notice that both
columns qrycol1 and qrycol2 are included in the queryexpr. Note, qrycol2 in the queryexpr
refers to qrycol1 in the input_query, and qrycol1 in the queryexpr refers to qrycol2 in the
input_query. As stated, order is significant, not the names.
CALL SYSLIB.JSON_SHRED_BATCH(
'SELECT JSONDOCID, JSONDT1, qrycol1, qrycol2 FROM
jsonshred.JSON_TABLE3 WHERE JSONID=100',
'[ { "rowexpr" : "$.population.profile",
"colexpr" : [{"col1" : "$.name.first", "type" : "VARCHAR(30)"},
{"col2" : "$.address.zip",
"type" : "NUMBER(5,0)"}],
"queryexpr" : [{ "qrycol2" : "VARCHAR(20)"},
{ "qrycol1" : "VARCHAR(20)"}],
"tables" : [
{"jsonshred.JSON_SHRED_TABLE1" : {
"metadata" : { "operation" : "insert" },
"columns" : {"EmpID":"JSONID*10",
"NAME":"col1","STATE":"qrycol1", "ZIP":"col2"}
}
}
]
}
]',res );
JSONID and ROWINDEX (uppercase or lowercase) are not allowed in colexpr and queryexpr
because they are fixed temporary column names. A syntax error is reported if they are used
in those clauses.
table_expression
The following describes the table expression variables.
• "tables" :
Required, literal entry.
Must be lowercase.
• "table_name" – The fully qualified name of an existing database table. The user invoking
JSON_SHRED_BATCH must have the required privileges (INSERT, UPDATE, and so
forth) on this table.
JSONID and ROWINDEX (uppercase or lowercase) are keywords. They are used to track the
input JSON document ID value (the first column of the input_query) and the index number for
an input row, respectively. JSONID and ROWINDEX may be referenced in the table_expression
clause as a source value for the shredding operation.
Note:
In the process of shredding, a volatile table is created for each shred statement. A table
can have a maximum of 2048 columns, so all the columns together from all the table
mappings should not exceed 2044 columns (there are four internal columns). You can
have 1 to N target tables, which can each have 1 to N columns, but the total number of
all columns must not exceed 2044.
metadata
The following describes the metadata variables.
• "metadata" :
Required, literal entry.
Must be lowercase.
• "operation" – Required, literal entry.
• "insert" | "update" | "merge" | "delete"
Operation to perform.
In a MERGE operation, the target table must have a primary index, and the primary index
has to be a member of the keys in the metadata.
• "keys":
Using keys is optional. If used, "keys": is a required, literal entry.
Note:
All names given in the keys clause must be present in the column
assignment clause.
The keys are used to perform the join between the temporary table created by the row
and column expressions and the target table. This should be used carefully as it can
drastically affect performance. In the case of a MERGE operation, the target table must
have a primary index, and the primary index has to be a member of the specified keys.
• "table_column_name" – The name of any column in the table referenced by table_name.
The user invoking JSON_SHRED_BATCH must have the required privileges (INSERT,
UPDATE, and so forth) on this existing table. table_name is specified in the table
expression of JSON_SHRED_BATCH.
• "filter": – Filtering is optional. If used, "filter": is a required, literal entry.
• filter_expression
SQL statement referencing elements of the column or query expressions.
Example filter statement: "filter" : "empId<5000"
column_assignment
The following describes the column assignment variables.
• "columns" : – Required, literal entry.
• "table_column_name'" – The name of any column in the table referenced by
table_name. The user invoking JSON_SHRED_BATCH must have the required
privileges (INSERT, UPDATE, and so forth) on this existing table. table_name
must be the fully qualified table name. It is specified in the table expression
of JSON_SHRED_BATCH.
• temp_column_name – A temp_column_name defined in "colexpr" or "queryexpr". The
temporary column name must be unique or an error is reported. Note: Temporary
column names are not case sensitive, so col1 and COL1 are not unique and will cause
an error.
• "temp_expr" – Teradata SQL expression.
• numeric_constant – Any JSON-supported Numeric value.
• ["string_constant"]
Any JSON-supported string value.
Example string constant: "company" : ["Teradata"]
• boolean_constant
true or false
true and false are JSON keywords and must be lowercase.
• null
JSON null.
null is a JSON keyword and must be lowercase.
JSON
Usage Notes
Note:
When a JSON value is shredded to populate a CHAR, VARCHAR, or VARBYTE column, if the size
of the value is larger than the size of the target column, the value is truncated to fit the column.
The JSON_SHRED_BATCH query provides flexibility between the source JSON instance and the table(s)
the source data is loaded into. This flexibility allows for efficient and non-efficient queries, depending on
the query itself and how the mapping (shred statement) is performed.
The following guidelines assist in achieving the optimal performance with these procedures.
• For each shred statement, a JSON_TABLE function call is made, to shred the JSON object into a
temporary table based on the row expression and column expressions. The resulting temporary table
may be used to assign values to any column of any table for which the user has the proper privileges.
The best performing queries optimize the mapping such that each shred statement updates the
maximum possible number of tables. Only if complications of the mapping (such as hierarchical
relationships) make it impossible to map a shredding to an actual column should another shred
statement be included in the query.
• The performance is largely dependent upon the usage of the procedure. If the mapping minimizes
the number of separate queries needed, it will perform best. It is not always the case that everything
can fit into one shred statement; for this reason multiple statements are allowed.
• This procedure allows INSERT, UPDATE, MERGE and DELETE operations, which can be specified
in the operation portion of the metadata portion of the statement. The keys in the metadata statement
are used to perform the join between the temporary table created by the row/column expressions and
the target table. This should be used carefully as it can drastically affect performance. In a MERGE
operation, the target table must have a primary index, and the primary index has to be a member of
the keys in the metadata.
• In order to avoid a full table join, we require an ID column to be specified in the input query parameter,
so that a join condition can be built off that column.
Columns of a target table may be assigned values in the temporary table created by the row and column
expressions, constants, or the results of SQL expressions. The use of an SQL expression requires the
user to submit a proper SQL statement (in terms of syntax and actual results of the query). This is a
powerful and flexible way to manipulate the data in a target table, but can cause a problem if queries are
not structured properly. Any errors reported by the DBS based on an SQL expression will be reported
to the user and cause the query to fail. Columns of the temporary table created by the row and column
expressions and the extra columns created by the input query may be used in the SQL expression.
In the process of shredding, a volatile table is created for each shred statement. A table can have a
maximum of 2048 columns, so all the columns together from all the table mappings should not exceed
2044 columns (there are four internal columns). You can have 1 to N target tables, which can each have
1 to N columns, but the total number of all columns must not exceed 2044.
All keywords in the shred statement must be specified in lowercase.
The names assigned to the temporary columns (temp_column_name) and the names of extra columns
created by the input query must be unique. They can be referenced in the table expression clause, so
there cannot be any ambiguity. Note, names are not case sensitive. If a non-unique name is detected, an
error is reported. For example, col1 and COL1 will fail because they are used in internal queries and are
not unique.
Note:
All the names given in the keys clause must be present in the column assignment clause.
You must specify the data type of the temporary column in the output table in the column expression. It
is necessary to provide this information so that the data may be correctly interpreted and used with the
target table(s).
JSONID and ROWINDEX (uppercase or lowercase) are keywords. They are used to track the input JSON
document ID value (the first column of the input query) and the index number for an input row, respectively.
JSONID and ROWINDEX are not allowed in colexpr and queryexpr because they are fixed temporary column
names. A syntax error is reported if they are used in those clauses. However, they may be referenced in
the table expression clause as a source value for the shredding operation.
The example populates a table using a JSON instance as the source data. The example shreds the JSON
document to extract values from it and inserts the data into the employee table (emp_table).
Note:
Before running this example, make sure you have SELECT privilege on the source table
( json_table) and INSERT privilege on the target table (emp_table).
CALL SYSLIB.JSON_SHRED_BATCH(
'SELECT id, empPersonalInfo, site
FROM test.json_table',
'[{"rowexpr" : "$.employees.info[*]",
"colexpr" : [{"col1" : "$.id",
"type" : "INTEGER"},
{"col2" : "$.employees.company",
"type" : "VARCHAR(15)",
"fromRoot" : true},
{"col3" : "$.name",
"type" : "VARCHAR(20)"},
{"col4" : "$.age",
"type" : "INTEGER"},
{"col5" : "$.dept",
"type" : "VARCHAR(10)"}],
"queryexpr" : [{"site" : "VARCHAR(20)"}],
"tables" : [
{"test.emp_table" : {
"metadata" : {
"operation" : "insert"
},
"columns" : {"empID" : "col1*100",
"company" : "col2",
"empName" : "col3",
"empAge" : "col4",
"dept" : "col5",
"startDate" : "CURRENT_DATE",
"site" : "site" }
}
}]
}]', :res );
The result of the shred populates the emp_table table with three rows, corresponding to the three items
in the JSON object used as source data.
To see the result, run: SELECT empID, company, empName, empAge, startDate, site FROM
emp_table ORDER BY empID;
Related Information:
Setting up the JSON_SHRED_BATCH Examples
The example uses the JSON_SHRED_BATCH Update operation to update a table from a JSON instance.
Assume some new data comes in which provides the actual start date of the three employees (previously
we loaded the table with a default value). We can update that specific value using JSON_SHRED_BATCH
with the query below.
Note:
Before running this example, make sure you have SELECT privilege on the source table
( json_table) and UPDATE privilege on the target table (emp_table).
CALL SYSLIB.JSON_SHRED_BATCH(
'SELECT id, empCompanyInfo FROM test.json_table',
'[
{
"rowexpr" : "$.startDates.info[*]",
"colexpr" : [
{"col1" : "$.id",
"type" : "INTEGER"},
{"col2" : "$.startDates.company",
"type" : "VARCHAR(15)",
"fromRoot" : true},
{"col3" : "$.startDate",
"type" : "VARCHAR(20)"}
],
"tables" : [
{
"test.emp_table" : {
"metadata" : {
"operation" : "update",
"keys" : [ "empID", "company" ]
},
"columns" : { "empID" : "col1*100",
"company" : "col2",
"startDate" : "col3" }
}
}
]
}
]', :res );
Result: To view the updated data in the employee table, run: SELECT empID, company, empName,
empAge, startDate, site FROM emp_table ORDER BY empID;
Related Information:
Setting up the JSON_SHRED_BATCH Examples
The example uses a single JSON_SHRED_BATCH call to populate two tables with data.
Note:
Before running this example, make sure you have SELECT privilege on the source table
( json_table) and INSERT privilege on the target tables (emp_table, dept_table).
CALL SYSLIB.JSON_SHRED_BATCH(
'SELECT id, empPersonalInfo, site FROM test.json_table',
'[
{
"rowexpr" : "$.employees.info[*]",
"colexpr" : [
{"col1" : "$.id",
"type" : "INTEGER"},
{"col2" : "$.employees.company",
"type" : "VARCHAR(15)",
"fromRoot" : true},
{"col3" : "$.name",
"type" : "VARCHAR(20)"},
{"col4" : "$.age",
"type" : "INTEGER"},
{"col5" : "$.dept",
"type" : "VARCHAR(20)"}
],
"queryexpr" : [
{"site" : "VARCHAR(20)"}
],
"tables" : [
{
"test.emp_table" : {
"metadata" : { "operation" : "insert" },
"columns" : {
"empID" : "col1*100",
"company" : "col2",
"empName" : "col3",
"empAge" : "col4",
"dept" : "col5",
"startDate" : "CURRENT_DATE",
"site" : "site" }
}
},
{
"test.dept_table" : {
"metadata" : { "operation" : "insert" },
"columns" : {
"dept" : "col5",
"description" : ["CONSTANT DESCRIPTION"],
"empID" : "col1"
}
}
}
]
}
]', :res );
The result of the above shred will populate the emp_table and dept_table tables with three rows,
corresponding to the three items in the JSON object used as source data.
Result: To view the data inserted into the employee table, run: SELECT * FROM emp_table ORDER
BY empID;
Result: To view the data inserted into the department table, run: SELECT * FROM dept_table ORDER BY
dept, empID;
Related Information:
Setting up the JSON_SHRED_BATCH Examples
Note:
Before running this example, make sure you have SELECT privilege on the source table
(jsonshred.JSONDocs) and INSERT privilege on the target table (jsonshred.Teradata_Employees).
CALL SYSLIB.JSON_SHRED_BATCH(
'SELECT * FROM jsonshred.JSONDocs',
NEW JSON('[
{
"rowexpr" : "$.employees.info[*]",
"colexpr" : [{"col1" : "$..id", "type" : "INTEGER"},
{"col2" : "$.employees.company",
"type" : "VARCHAR(15)","fromRoot":true},
{"col3" : "$..name", "type" : "VARCHAR(20)"},
{"col4" : "$..dept", "type" : "VARCHAR(20)"}],
"queryexpr" : [{"qrycol1" : "varchar(20)"},{"qrycol2" : "varchar(20)"}],
"tables" : [
{"jsonshred.Teradata_Employees" : {
"metadata" : { "operation" : "insert","keys" : ["empId"] },
"columns" : {"rowIndex" : "ROWINDEX",
"empId" : "col1+1000",
"empName" : "col3",
"jsonDocId" : "JSONID",
"company" : "col2",
"dept" : "col4",
"site" : "qrycol1",
"country" : "qrycol2" }
}
}
]
}]', LATIN), :res);
Related Information:
Setting Up the JSON_SHRED_BATCH JSONID and ROWINDEX Keyword Example
This is very useful if loading data into a table which uses one of these storage formats, or exporting the
data to an external system which recognizes these formats.
The above query is equivalent to the following query which uses the JSON_COMPOSE function to generate
the JSON document:
• If you specify TOP n or ORDER BY in the SELECT statement, you must explicitly specify the sorted
fields because you cannot sort by the JSON column.
For more information about the SELECT AS JSON statement, see "Select Statements" in Teradata
Vantage™ - SQL Data Manipulation Language, B035-1146.
Similarly, if you specify TOP n in the SELECT statement, you must explicitly specify the field being sorted:
SELECT AS JSON TOP 2 pkey, val FROM MyTable ORDER BY pkey DESC;
The following query fails because comparisons are not allowed for the JSON type.
The following query fails because sorting is not supported for the JSON type.
JSON_AGG
The JSON_AGG function returns a JSON document composed of aggregated values from each input
parameter. The input parameters can be a column reference or an expression. Each input parameter results
in a name/value pair in the returned JSON document.
JSON_AGG Syntax
{ JSON_AGG ( param_spec [,...] ) |
Syntax Elements
param_spec
returns_clause
STYLE column_expr
}
param
An input parameter that can be any supported data type, a column reference, constant,
or expression that evaluates to some value. A variable number of these parameters
are accepted and each input parameter results in a name/value pair in the returned
JSON document.
Note:
You cannot use structured UDTs with LOB attributes as input.
FORMAT 'format_string'
format_string is any allowable format string in Vantage.
For an example using the format_string see Example: Use JSON_COMPOSE with
Subqueries and GROUP BY.
AS name
name is any allowable name in Vantage.
The string created conforms to the JSON standard escaping scheme. A subset of UNICODE
characters are required to be escaped by the '\' character. This is not the case for strings
in Vantage. Thus, when porting a Vantage string to a JSON string, proper JSON escape
characters are used where necessary. This also applies to the values of the JSON instance
and to the JSON_COMPOSE function. If the character set is LATIN, '\' escaped characters
must be part of that character set; otherwise a syntax error is reported.
RETURNS data_type
Specifies that data_type is the return type of the function.
data_type must be JSON for this function.
integer
A positive integer value that specifies the maximum length in characters of the JSON type.
If specified, the length is subject to a minimum of two characters and cannot be greater than
the absolute maximum allowed for the function. Shorter lengths may be specified.
Note:
As an aggregate function, JSON_AGG supports up to 64000 bytes, which is 32000
UNICODE characters or 64000 LATIN characters. The RETURNS clause can specify
a larger return value, but the actual data returned by JSON_AGG is 64000 bytes. If
the data length is greater than this an error is returned. Note, JSON_COMPOSE can
specify larger values than JSON_AGG.
If you do not specify a RETURNS clause, the return type defaults to JSON(32000)
CHARACTER SET UNICODE. In other words, the default return type is a JSON data
type with UNICODE character set and a return value length of 32000 characters.
Result Type
By default, JSON_AGG returns a JSON document in character set UNICODE and a maximum
length of 32000 UNICODE characters (64000 bytes), unless otherwise specified with the optional
RETURNS clause.
A hierarchical relationship is not possible with this function. The resulting JSON instance is flat, with
each input parameter corresponding to one child of the result. The resulting document will be in the
following format.
{
name1 : data1,
name2 : data2,
...,
nameN : dataN,
}
If one of the values used to compose the JSON document is a Vantage NULL, it is returned in the JSON
instance as a JSON null.
Usage Notes
The GROUP BY clause can be used in the SELECT statement which invokes the JSON_AGG function.
Existing rules for the GROUP BY clause and aggregate functions apply to JSON_AGG. When this is used,
the resulting JSON document is structured as an array with objects as its elements that represent members
of the resulting group. Each group is in a different output row.
If one of the values used to compose the JSON object is a Vantage NULL, it is returned in the JSON
instance as a JSON null.
JSON_AGG writes out the values of all attributes of a structured UDT in a comma-separated list enclosed
in parenthesis. For example: (att1Val, att2Val, ..., attNVal). If this cannot be done in 64000 bytes, an SQL
error is reported and the query fails.
Input character data can contain escape characters that have not been correctly escaped to conform to
JSON syntax. When the function encounters these unescaped characters, it will encode the input so that
it conforms to proper JSON syntax. However, a syntax error will still be returned if after escaping, the
truncated data results in incorrect JSON syntax.
Note:
All non-predefined types, such as all UDTs, use their transformed value to populate the resulting
JSON document. The user must provide a transform that outputs data in valid JSON syntax in order
to function properly. Otherwise, validation of the JSON instance will fail and the function returns an
error message. However, as stated above, the function will accept unescaped characters and will not
return an error in this case.
The data type in the JSON value is determined according to the mapping above, based on the
predefined data type of the result of the transform. All non-LOB predefined types are formatted
according to the optional FORMAT clause specified for any particular column, or if that is not present,
the default format for the particular data type.
The param name can be specified using the optional "AS" clause for each parameter. If the optional portion
is NULL, the names of the parameters that make up the resulting JSON document are given according to
current naming conventions of expressions in Vantage.
JSON_AGG Examples
The example selects columns from a table and uses JSON_AGG to compose those columns into
JSON objects.
Result:
JSON_agg
--------
[{ "empID" : 1, "company" : "Teradata", "empName" : "Cameron", "empAge" : 24 },
{ "empID" : 2, "company" : "Teradata", "empName" : "Justin", "empAge" : 34 },
{ "empID" : 3, "company" : "Teradata", "empName" : "Someone", "empAge" : 24 }]
Related Information:
Setting Up the JSON_AGG Examples
The example shows how to use JSON_Agg to assign parameter names in the resulting JSON object.
Result:
JSON_agg
--------
{ "id" : 1, "company" : "Teradata", "name" : "Cameron", "age" : 24 },
Related Information:
Setting Up the JSON_AGG Examples
The example shows how to use JSON_Agg to assign parameter names in the resulting JSON instance
and use the GROUP BY clause.
Result: The query returns one line of output (note, output line is wrapped).
Related Information:
Setting Up the JSON_AGG Examples
The example shows how to assign parameter names and use the GROUP BY clause.
Result:
{"id":2,"name":"Justin"}
[{"id":3,"name":"Someone"},{"id":1,"name":"Cameron"}]
Related Information:
Setting Up the JSON_AGG Examples
JSON_COMPOSE
JSON_COMPOSE creates a JSON document composed of the input parameters specified. This
function provides a complex composition of a JSON document when used in conjunction with the
JSON_AGG function.
JSON_COMPOSE Syntax
{ JSON_COMPOSE ( param_spec [,...] ) |
Syntax Elements
param_spec
returns_clause
STYLE column_expr
}
param
An input parameter that can be a column reference, constant, or expression that evaluates to
some value. A variable number of these parameters are accepted, and each input parameter
results in a name/value pair in the returned JSON document.
Note:
You cannot use structured UDTs with LOB attributes as input.
FORMAT 'format_string'
format_string is any allowable format string in Vantage.
For an example using the format_string see Example: Use JSON_COMPOSE with
Subqueries and GROUP BY.
AS name
name is any allowable name in Vantage.
The string created conforms to the JSON standard escaping scheme. A subset of UNICODE
characters are required to be escaped by the '\' character. This is not the case for strings
in Vantage. Thus, when porting a Vantage string to a JSON string, proper JSON escape
characters are used where necessary. This also applies to the values of the JSON instance
and to the JSON_AGG function. If the character set is LATIN, '\' escaped characters must be
part of that character set; otherwise a syntax error is reported.
RETURNS data_type
Specifies that data_type is the return type of the function.
data_type must be JSON for this function.
integer
A positive integer value that specifies the maximum length in characters of the JSON type.
If specified, the length is subject to a minimum of two characters and cannot be greater than
the absolute maximum allowed for the function. Shorter lengths may be specified.
Note:
As an aggregate function, JSON_AGG supports up to 64000 bytes, which is 32000
UNICODE characters or 64000 LATIN characters. The RETURNS clause can specify
a larger return value, but the actual data returned by JSON_AGG is 64000 bytes. If
the data length is greater than this an error is returned. Note, JSON_COMPOSE can
specify larger values than JSON_AGG.
If you do not specify a RETURNS clause, the return type defaults to JSON(32000)
CHARACTER SET UNICODE. In other words, the default return type is a JSON data
type with UNICODE character set and a return value length of 32000 characters.
Result Type
By default, JSON_COMPOSE returns a LOB based JSON document with character set UNICODE and
a maximum length of 32000 UNICODE characters (64000 bytes), unless otherwise specified with the
optional RETURNS clause.
A hierarchical relationship is not possible with this function. The resulting JSON document is flat, with each
input parameter corresponding to one child of the result. The resulting document will be in this format:
{
name1 : data1,
name2 : data2,
...,
nameN : dataN,
}
If one of the values used to compose the JSON document is a Vantage NULL, it is returned in the JSON
instance as a JSON null.
Usage Notes
JSON_COMPOSE is most useful when used in conjunction with JSON_AGG. JSON_AGG is limited in that
it provides groups as identified by the GROUP BY clause, but it does not provide the value that was used to
create the group. To obtain this, use JSON_AGG in a subquery that results in a derived table, and reference
the result of JSON_AGG as one of the parameters to the JSON_COMPOSE function. To ensure the values
being grouped on are included with the proper groups, the columns used in the GROUP BY clause of the
subquery with the JSON_AGG function should be used as parameters to the JSON_COMPOSE function
along with the result of JSON_AGG. In this way, the values being grouped on will be included alongside
the group.
JSON_COMPOSE writes out the values of all attributes of a structured UDT in a comma-separated list
enclosed in parenthesis. For example: (att1Val, att2Val, ..., attNVal). If this cannot be done in 64000 bytes,
an SQL error is reported and the query fails.
Input character data can contain escape characters that have not been correctly escaped to conform to
JSON syntax. When the function encounters these unescaped characters, it will encode the input so that
it conforms to proper JSON syntax. However, a syntax error will still be returned if after escaping, the
truncated data results in incorrect JSON syntax.
Note:
All non-predefined types, such as all UDTs, use their transformed value to populate the resulting
JSON document. The user must provide a transform that outputs data in valid JSON syntax in order
to function properly. Otherwise, validation of the JSON instance will fail and the function returns an
error message. However, as stated above, the function will accept unescaped characters and will not
return an error in this case.
The data type in the JSON value is determined according to the mapping above, based on the
predefined data type of the result of the transform. All non-LOB predefined types will be formatted
according to the optional FORMAT clause specified for any particular column, or if that is not present,
the default format for the particular data type.
The param name can be specified using the optional "AS" clause for each parameter. If the optional portion
is NULL, the names of the parameters that make up the resulting JSON document will be given according
to current naming conventions of expressions in Vantage.
JSON_COMPOSE Examples
Use JSON_COMPOSE to create a JSON object from the values selected from a table.
Result:
JSON_Compose
------------
{
"company" : "Teradata",
"employees" : [
{ "id" : 1, "name" : "Cameron", "age" : 24 },
{ "id" : 2, "name" : "Justin", "age" : 34 }
]
}
{
"company" : "Apple",
"employees" : [
{ "id" : 3, "name" : "Someone", "age" : 24 }
]
}
Related Information:
Setting Up the JSON_COMPOSE Basic Examples
The example uses JSON_AGG to aggregate employee ID and employee name, and uses
JSON_COMPOSE to create a JSON object from the values extracted and aggregated.
(
SELECT company, empAge,
JSON_agg(empID AS id,
empName AS name) AS employees
FROM emp_table
GROUP BY company, empAge
) AS T;
Result:
JSON_Compose
------------
{
"company" : "Teradata",
"age" : 24,
"employees" : [
{ "id" : 1, "name" : "Cameron" }
]
}
{
"company" : "Teradata",
"age" : 34,
"employees" : [
{ "id" : 2, "name" : "Justin" }
]
}
{
"company" : "Apple",
"age" : 24,
"employees" : [ { "id" : 3, "name" : "Someone" } ]
}
Related Information:
Setting Up the JSON_COMPOSE Basic Examples
orderID INTEGER,
itemID INTEGER,
itemName VARCHAR(50),
amount INTEGER);
SELECT JSON_Compose(O.customer,
O.orderID,
O.price,
I.JA AS items)
FROM
(
SELECT customer AS customer,
orderId AS orderID,
price (FORMAT '$(9).9(2)') AS price
FROM order_table
) AS O,
(
SELECT orderID AS orderID,
JSON_Agg(itemID AS ID,
itemName AS name,
amount AS amt) AS JA
FROM item_table
GROUP BY orderID
) AS I
WHERE O.orderID = I.orderID;
Result:
JSON_Compose
------------
{
"customer" : "Teradata",
"orderID" : 1,
"price" : "$1000.00",
"items" :
[
{ "ID" : 1, "name" : "disk", "amt" : 100 },
{ "ID" : 2, "name" : "RAM", "amt" : 200 },
]
}
{
"customer" : "Teradata",
"orderID" : 2,
"price" : "$10000.00",
"items" :
[
{ "ID" : 1, "name" : "disk", "amt" : 10 },
{ "ID" : 2, "name" : "RAM", "amt" : 20 },
{ "ID" : 3, "name" : "monitor", "amt" : 30 },
{ "ID" : 4, "name" : "keyboard", "amt" : 40 }
]
}
{
"customer" : "Apple",
"orderID" : 3,
"price" : "$100000.00",
"items" :
[
{ "ID" : 1, "name" : "disk", "amt" : 10 },
{ "ID" : 2, "name" : "RAM", "amt" : 20 },
{ "ID" : 3, "name" : "monitor", "amt" : 30 },
{ "ID" : 4, "name" : "keyboard", "amt" : 40 },
{ "ID" : 5, "name" : "camera", "amt" : 50 },
Related Information:
Setting up the JSON_COMPOSE Advanced Examples
SELECT JSON_Compose(T.customer,
T.JA AS orders)
FROM
(
SELECT O.customer AS customer,
JSON_Agg(O.orderID, O.price, I.JA AS items) AS JA
FROM
(
SELECT customer AS customer,
orderId AS orderID,
price AS price
FROM order_table
) AS O,
(
SELECT orderID AS orderID,
JSON_Agg(itemID AS ID,
itemName AS name,
amount AS amt) AS JA
FROM item_table
GROUP BY orderID
) AS I
WHERE O.orderID = I.orderID
GROUP BY O.customer
) AS T;
Result:
> JSON_Compose
------------
{
"customer" : "Teradata",
"orders" :
[
{
"orderID" : 1,
"price" : 1000,
"items" :
[
{ "ID" : 1, "name" : "disk", "amt" : 100 },
{ "ID" : 2, "name" : "RAM", "amt" : 200 }
]
},
{
"orderID" : 2,
"price" : 10000,
"items" :
[
{ "ID" : 1, "name" : "disk", "amt" : 10 },
{ "ID" : 2, "name" : "RAM", "amt" : 20 },
{ "ID" : 3, "name" : "monitor", "amt" : 30 },
{ "ID" : 4, "name" : "keyboard", "amt" : 40 }
]
}
]
}
{
"customer" : "Apple",
"orders" :
[
{
"orderID" : 3,
"price" : 100000,
"items" :
[
{ "ID" : 1, "name" : "disk", "amt" : 10 },
{ "ID" : 2, "name" : "RAM", "amt" : 20 },
{ "ID" : 3, "name" : "monitor", "amt" : 30 },
{ "ID" : 4, "name" : "keyboard", "amt" : 40 },
{ "ID" : 5, "name" : "camera", "amt" : 50 },
{ "ID" : 6, "name" : "button", "amt" : 60 },
{ "ID" : 7, "name" : "mouse", "amt" : 70 },
{ "ID" : 8, "name" : "pen", "amt" : 80 }
]
}
]
}
Related Information:
Setting up the JSON_COMPOSE Advanced Examples
JSON_PUBLISH
JSON_PUBLISH is a table operator that is used to compose a JSON data type instance or instances from a
variety of data sources, basically anything that can be referenced in an SQL statement. It can publish data
into any supported JSON format, including the binary storage formats. JSON_PUBLISH also has the ability
to aggregate data larger than 64K into a JSON document.
JSON_PUBLISH returns one output column called data, which is the resulting JSON document. The return
type of this table operator is always a maximum size JSON data type.
JSON_PUBLISH can compose data into a JSON data type of any character set or storage format using the
RETURNS clause of the table operators. The default is to return maximum size JSON CHARACTER SET
LATIN instances.
JSON_PUBLISH Syntax
JSON_PUBLISH uses the standard table operator syntax. For details, see the table operator syntax
described as part of the SELECT statement in Teradata Vantage™ - SQL Data Manipulation Language.
DO_AGGREGATE
Causes the output instance(s) to be composed of one row of input data. This clause accepts
either 'Y' or 'N' (not case specific) as follows:
• 'Y' specifies that the result is aggregated. This is the default.
• 'N' specifies that the result is not aggregated.
Excluding this clause causes JSON_PUBLISH to aggregate all data corresponding to a
particular group (as defined in the optional GROUP BY clause in the SELECT statement
of the ON clause) into one JSON data type instance. Note that the absence of this custom
clause and of a GROUP BY clause causes all input rows to be grouped together in one
resulting instance. If the result of aggregating the data into a JSON data type instance
results in a size overflow based on the maximum size specified, an error accompanied by an
appropriate error string is reported.
WRITE_ARRAY
Causes the output instance(s) to not be JSON arrays at the top level. Therefore, the default
is to return a JSON array composed of one or more JSON documents which represent the
published data. The only value accepted by this clause is the character 'N' (not case specific).
If you do not group the published data into a JSON array, but failure to do so would violate
JSON syntax, an error accompanied by an appropriate error string is reported.
Examples: JSON_PUBLISH
Setting Up the JSON_PUBLISH Examples
This simple table is used to illustrate the functionality of JSON_PUBLISH in the following examples.
Result:
data
----------------------------------------------------
[{
"empID": 3,
"empName": "Steven Mazzo",
"empDept": "Engineering"
},
{
"empID": 1,
"empName": "George Smith",
"empDept": "Accounting"
},
{
"empID": 2,
"empName": "Pauline Kramer",
"empDept": "HR"
}]
Result:
data
----------------------------------------------------
[{
"empID": 1,
"empName": "George Smith",
"empDept": "Accounting"
}]
----------------------------------------------------
[{
"empID": 2,
"empName": "Pauline Kramer",
"empDept": "HR"
}]
----------------------------------------------------
[{
"empID": 3,
"empName": "Steven Mazzo",
"empDept": "Engineering"
}]
----------------------------------------------------
FROM employeeTable)
USING DO_AGGREGATE('N')
) AS jsonData ORDER BY data.."ID";
Results:
data
----------------------------------------------------
[{
"ID": 1,
"Name": "George Smith",
"Dept": "Accounting"
}]
----------------------------------------------------
[{
"ID": 2,
"Name": "Pauline Kramer",
"Dept": "HR"
}]
----------------------------------------------------
[{
"ID": 3,
"Name": "Steven Mazzo",
"Dept": "Engineering"
}]
----------------------------------------------------
Publish the entire table, without aggregating, without building JSON array, using the UNICODE
character set.
Results:
col1
---------------------------------------------------------------
{"empID":1,"empName":"George Smith","empDept":"Accounting"}
---------------------------------------------------------------
{"empID":2,"empName":"Pauline Kramer","empDept":"HR"}
---------------------------------------------------------------
{"empID":3,"empName":"Steven Mazzo","empDept":"Engineering"}
---------------------------------------------------------------
JSON_PUBLISH may be used to aggregate multiple values from multiple rows into one instance. However,
this aggregation is done locally on each AMP within Vantage. Therefore, JSON_PUBLISH may be invoked
twice within a statement to perform a single aggregation of all input values. If JSON_PUBLISH is invoked
once without the PARTITION BY clause, each AMP produces one row of aggregated output.
The following INSERT statements add some input data to the example table to illustrate this point. Note
that actual aggregation in this initial step depends on the architecture of the system on which it is run.
Results:
data
----------------------------------------------------
[{
"empID": 5,
"empName": "Kyle Newman",
"empDept": "Engineering"
},
{
"empID": 3,
"empName": "Steven Mazzo",
"empDept": "Engineering"
},
{
"empID": 1,
"empName": "George Smith",
"empDept": "Accounting"
},
{
"empID": 2,
"empName": "Pauline Kramer",
"empDept": "HR"
}]
----------------------------------------------------
[{
"empID": 4,
"empName": "Jose Hernandez",
"empDept": "Engineering"
}]
----------------------------------------------------
[{
"empID": 6,
"empName": "Pamela Giles",
"empDept": "Sales"
}]
----------------------------------------------------
To do a final union from all AMPs, the local results from each AMP must be aggregated. This may be
achieved by nesting a second call to JSON_PUBLISH. The inner call to JSON_PUBLISH performs the
local aggregation on each AMP as described above, and the outer call will do a final aggregation of the local
aggregations and produce a single result row that represents the union of all input values.
Use the PARTITION BY clause with a constant value (for example, a value of 1) to perform the final
aggregation on a single AMP. By specifying a constant value, like the number 1, the locally aggregated rows
from each AMP will be in the same partition and as a result will all be redistributed to the same AMP for the
final aggregation. The outer query will partition by this constant shown as "1 as p" below. A single output
row will be returned.
Additionally, the aggregated result may be referenced using dot notation by using the recursive descent
operator '..' to reference the name of the result of the inner JSON_PUBLISH query followed by an array
reference composed of the '*' wildcard, which will retrieve one array composed of one record per input row.
The final query would look something like the following:
Results:
data..record[*]
----------------------------------------------------
[{
"empID": 5,
Note that you could aggregate all values with a single call to JSON_PUBLISH by partitioning by a constant
value, but that would redistribute all rows to a single AMP to perform the aggregation, which would not
take advantage of the parallel processing capability of Vantage. By using two calls to JSON_PUBLISH,
the AMPs will perform local aggregations in parallel and only the final aggregation will be performed on a
single AMP.
String of one or more digits. Do not use commas in numbers with more than
number three digits.
Example: 10045
x is optional.
[ x ]
Note:
You can repeat only the immediately preceding item. For example, if the syntax is:
KEYWORD x [...]
[ x, [...] ] y
Character Symbols
The symbols, along with character sets with which they are used, are defined in the following table.
Symbol Encoding Meaning
For example, string “TEST”, where each letter is intended to be a fullwidth character, is written as TEST.
Occasionally, when encoding is important, hexadecimal representation is used.
For example, the following mixed single byte/multibyte character data in KanjiEBCDIC character set:
LMN<TEST>QRS
is represented as:
D3 D4 D5 0E 42E3 42C5 42E2 42E3 0F D8 D9 E2
Pad Characters
The following table lists the pad characters for the various character data types.
Server Character Set Pad Character Name Pad Character Value
Conversion Rules
JSON stored as text, BSON, or UBJSON can contain specific data types depending on the storage format
specification. The following table lists all extended data types of each binary storage specification and their
corresponding representation in all other specifications.
Source Target
Specification Source Type Specification Target Type
UBJSON float32/float64
{"$type": <string
representing type of
binary data>,
"$binary": <string
representing binary data>}
{"$oid": <string
representing object id>}
{"$date": <number
representing date>}
{"$ref": <string
representing collection>,
Source Target
Specification Source Type Specification Target Type
"$id": <string
representing object id>}
{"$timestamp": {
"$t": <number
representing timestamp>,
"$i": <number representing
increment>} }
{"$minKey": 1}
{"$maxKey": 1}
UBJSON int32
UBJSON int64
BSON int32
BSON int32
BSON int32
BSON int64
Source Target
Specification Source Type Specification Target Type
BSON double
BSON double
These codes are sent from server to client, and are accepted by server from client in the parcels described
in the following sections. The only restriction is the type may not be used in the USING clause. VARCHAR/
CLOB can be used in the USING clause and when necessary, this data is implicitly cast to the JSON type.
typedef
struct PclCfgSQLCapFeatType {
PclCfgFeatureType SQLCap_Feature;
PclCfgFeatureLenType SQLCap_Length;
byte /* 0 */ SQLCap_UPSERT;
byte /* 1 */ SQLCap_ArraySupport;
.
.
.
byte /* 20 */ padbyte_boolean;
byte /* 21 */ SQLCap_JSON;
} PclCfgSQLCapFeatType;
7 -Database-limit
+0 (char) -Maximum parcel size
'0' -32767
'1' -65535
+1 (byte) UNUSED
+2 (16-bit int) -Maximum segment count
0 -Segments not supported
+4 (32-bit int) -Maximum segment size
+8 (32-bit int) -Max. avail. bytes in perm row
.
.
.
+124 (32-bit int) -Max. avail. bytes in response row
+128 (32-bit int) -Max. bytes in a JSON
StatementInfo Parcel
The following fields of the StatementInfo Parcel contain information relevant to a particular instance of the
JSON data type and show typical values that are expected for the JSON data type:
• Data Type Code = JSON Data Type, according to the table in the Data Type Encoding section.
• UDT indicator = 0 (JSON data type is treated as a system built-in type)
• Fully qualified type name length = 0
• Fully qualified type name = ""
• Field Size = the maximum possible length in bytes for this particular JSON instance
• Character Set Code = 1 or 2, depending on the character set for this particular JSON instance
• Maximum number of characters = number of characters of the JSON column - the same as number in
the column definition
• Case Sensitive: 'Y' (JSON is case specific)
The "Data Type Misc Info" field provides information about the storage format of a JSON type.
When executing the SELECT statement, the StatementInfo parcel looks like the following:
Column Index 2
As Name
Title jsoncol
Format
Default Value
Is Identity Column N
Is Definitely Writable Y
Is Nullable Y
Is Searchable Y
Is Writable Y
UDT Indicator 0
UDT Name
UDT Misc
Max Data Length 1086000 (for UTF16 Session Character Set; 543000 * 2 [export factor])
Digits 0
Interval Digits 0
Fractional Digits 0
Is CaseSensitive Y
Is Signed U
Is Key Column N
Is Unique N
Is Expression N
Is Sortable N
Parameter Type U
Struct Depth 0
Is Temporal Column 0
Teradata Links
Link Description