UDF Return JSON Dot Notation Example | VantageCloud Lake - Example: UDF Returning a Value Using Dot Notation (Level 1) - Teradata Vantage

Teradata® VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Vantage
Published
January 2023
Language
English (United States)
Last Update
2024-04-03
dita:mapPath
phg1621910019905.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
phg1621910019905

This UDF shows the use of dot notation syntax to retrieve the value of "thenum" from the JSON string.

REPLACE FUNCTION test_udf(string JSON(32000))
RETURNS VARCHAR(32000)
LANGUAGE SQL
CONTAINS SQL
DETERMINISTIC
SQL SECURITY DEFINER
COLLATION INVOKER
INLINE TYPE 1
RETURN string.thenum;
SELECT test_udf(new json('{ "thenum" : "10" , "name": {"firstname" : "abc" }}'));

Result:

test_udf( NEW JSON('{ "thenum" : "10" , "name": {"firstname"
---------------------------------------------------------------------------
10
CREATE TABLE test_data(x1 int, y1 json(32000));

INSERT INTO test_data(1,new json('{ "thenum" : "10" , "name": {"firstname" : "abc" }}'));
SELECT test_udf(y1) FROM test_data;

Result:

test_udf(y1)
---------------------------------------------------------------------------
10
SELECT test_udf(test_data.y1) FROM test_data;

Result:

test_udf(y1)
---------------------------------------------------------------------------
10
SELECT test_udf(test_database.test_data.y1) FROM test_data;

Result:

test_udf(y1)
---------------------------------------------------------------------------
10