JSON_VALUE UDF
Translation reference to convert Oracle JSON_VALUE function to Snowflake
Description
As per Oracle's documentation, this function uses the SQL/JSON Path Expression to request information about a portion of a JSON instance. The returning value is always a scalar value, else the function returns NULL by default.
JSON_VALUE
( expr [ FORMAT JSON ], [ JSON_basic_path_expression ]
[ JSON_value_returning_clause ] [ JSON_value_on_error_clause ]
[ JSON_value_on_empty_clause ][ JSON_value_on_mismatch_clause ]
)The JSON_VALUE_UDF is a Snowflake implementation of the JSONPath specification that uses a modified version of the original JavaScript implementation developed by Stefan Goessner.
Sample Source Patterns
Setup Data
Run these queries to run queries in the JSON_VALUE Patterns section.
Oracle
CREATE TABLE MY_TAB (
my_json VARCHAR(5000)
);
INSERT INTO MY_TAB VALUES ('{
"store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}');Snowflake
JSON_VALUE Patterns
Oracle
'$..book[0].title'
'Sayings of the Century'
'$..book[0,1 to 3,3]'
NULL
'$.store.book[*]?(@.category == "reference").title'
'Sayings of the Century'
'$..book[0]'
'MY ERROR MESSAGE'
'$..book[0].isbn'
'MY EMPTY MESSAGE'
'$..book[0].isbn'
ORA-40462: JSON_VALUE evaluated to no value
'$..book[0].isbn'
NULL
'$..book[0].isbn'
ORA-40462: JSON_VALUE evaluated to no value
'$..book[0].isbn'
NULL
'$..book[0].title'
'Sayings of the Century'
'$..book[0].title'
'Sayin'
'$..book[0].title'
'Sayings of the Century'
'$..book[0].title'
NULL
'$..book[0].title'
420
'$..book[0].title'
ORA-01858: a non-numeric character was found where a numeric was expected
'$..book[0].title'
ORA-40450: invalid ON ERROR clause
Snowflake
'$..book[0].title'
'Sayings of the Century'
'$..book[0,1 to 3,3]'
NULL
'$.store.book[*]?(@.category == "reference").title'
'Sayings of the Century'
'$..book[0]'
'MY ERROR MESSAGE'
'$..book[0].isbn'
'MY EMPTY MESSAGE'
'$..book[0].isbn'
"SSC_CUSTOM_ERROR - NO MATCH FOUND"
'$..book[0].isbn'
NULL
'$..book[0].isbn'
"SSC_CUSTOM_ERROR - NO MATCH FOUND"
'$..book[0].isbn'
NULL
'$..book[0].title'
'Sayings of the Century'
'$..book[0].title'
'Sayin'
'$..book[0].title'
'Sayings of the Century'
'$..book[0].title'
NULL
'$..book[0].title'
420
'$..book[0].title'
NOT SUPPORTED
'$..book[0].title'
NOT SUPPORTED
Known Issues
1. Returning Type Clause is not fully supported
Now, the only supported types when translating the functionality of the RETURNING TYPE clause are VARCHAR2, CLOB and NUMBER.
For all the other types supported by the original JSON_VALUE function, the JSON_VALUE_UDF will behave as if no RETURNING TYPE clause was specified.
Unsupported types:
DATETIMESTAMP [WITH TIME ZONE]SDO_GEOMETRYCUSTOM TYPE
2. ON MISMATCH Clause is not supported
Now, the ON MISMATCH clause is not supported, and a warning EWI is placed instead. Thus, the translated code will behave as if no ON MISMATCH clause was originally specified.
3. Complex filters are not supported
Complex filters with more than one expression will return null as they are not supported.
For example, with the same data as before, this JSON path $.store.book[*]?(@.category == "reference").title is supported and will return 'Sayings of the Century'.
However, $.store.book[*]?(@.category == "reference" && @.price < 10).title will return null since more than one expression is used in the filter.
Related EWIs
SSC-FDM-0028: Syntax not supported in Snowflake.
Last updated