Semi-Structured Functions
This section provides reference information for the semi-structured data functions in PlaidCloud Lakehouse.
JSON Parsing, Conversion & Type Checking:
JSON Query and Extraction:
JSON Data Manipulation:
Object Operations:
Type Conversion:
1 - AS_<type>
Strict casting VARIANT
values to other data types.
If the input data type is not VARIANT
, the output is NULL
.
If the type of value in the VARIANT
does not match the output value, the output is NULL
.
Analyze Syntax
func.as_boolean( <variant> )
func.as_integer( <variant> )
func.as_float( <variant> )
func.as_string( <variant> )
func.as_array( <variant> )
func.as_object( <variant> )
SQL Syntax
AS_BOOLEAN( <variant> )
AS_INTEGER( <variant> )
AS_FLOAT( <variant> )
AS_STRING( <variant> )
AS_ARRAY( <variant> )
AS_OBJECT( <variant> )
Arguments
Arguments | Description |
---|
<variant> | The VARIANT value |
Return Type
- AS_BOOLEAN: BOOLEAN
- AS_INTEGER: BIGINT
- AS_FLOAT: DOUBLE
- AS_STRING: VARCHAR
- AS_ARRAY: Variant contains Array
- AS_OBJECT: Variant contains Object
SQL Examples
SELECT as_boolean(parse_json('true'));
+--------------------------------+
| as_boolean(parse_json('true')) |
+--------------------------------+
| 1 |
+--------------------------------+
SELECT as_integer(parse_json('123'));
+-------------------------------+
| as_integer(parse_json('123')) |
+-------------------------------+
| 123 |
+-------------------------------+
SELECT as_float(parse_json('12.34'));
+-------------------------------+
| as_float(parse_json('12.34')) |
+-------------------------------+
| 12.34 |
+-------------------------------+
SELECT as_string(parse_json('"abc"'));
+--------------------------------+
| as_string(parse_json('"abc"')) |
+--------------------------------+
| abc |
+--------------------------------+
SELECT as_array(parse_json('[1,2,3]'));
+---------------------------------+
| as_array(parse_json('[1,2,3]')) |
+---------------------------------+
| [1,2,3] |
+---------------------------------+
SELECT as_object(parse_json('{"k":"v","a":"b"}'));
+--------------------------------------------+
| as_object(parse_json('{"k":"v","a":"b"}')) |
+--------------------------------------------+
| {"k":"v","a":"b"} |
+--------------------------------------------+
2 - CHECK_JSON
Checks the validity of a JSON document.
If the input string is a valid JSON document or a NULL
, the output is NULL
.
If the input cannot be translated to a valid JSON value, the output string contains the error message.
Analyze Syntax
Analyze Example
func.check_json('[1,2,3]');
+----------------------------+
| func.check_json('[1,2,3]') |
+----------------------------+
| NULL |
+----------------------------+
SQL Syntax
Arguments
Arguments | Description |
---|
<expr> | An expression of string type |
Return Type
String
SQL Examples
SELECT check_json('[1,2,3]');
+-----------------------+
| check_json('[1,2,3]') |
+-----------------------+
| NULL |
+-----------------------+
SELECT check_json('{"key":"val"}');
+-----------------------------+
| check_json('{"key":"val"}') |
+-----------------------------+
| NULL |
+-----------------------------+
SELECT check_json('{"key":');
+----------------------------------------------+
| check_json('{"key":') |
+----------------------------------------------+
| EOF while parsing a value at line 1 column 7 |
+----------------------------------------------+
3 - FLATTEN
import FunctionDescription from '@site/src/components/FunctionDescription';
Transforms nested JSON data into a tabular format, where each element or field is represented as a separate row.
SQL Syntax
[LATERAL] FLATTEN ( INPUT => <expr> [ , PATH => <expr> ]
[ , OUTER => TRUE | FALSE ]
[ , RECURSIVE => TRUE | FALSE ]
[ , MODE => 'OBJECT' | 'ARRAY' | 'BOTH' ] )
Parameter / Keyword | Description | Default |
---|
INPUT | Specifies the JSON or array data to flatten. | - |
PATH | Specifies the path to the array or object within the input data to flatten. | - |
OUTER | If set to TRUE, rows with zero results will still be included in the output, but the values in the KEY, INDEX, and VALUE columns of those rows will be set to NULL. | FALSE |
RECURSIVE | If set to TRUE, the function will continue to flatten nested elements. | FALSE |
MODE | Controls whether to flatten only objects ('OBJECT'), only arrays ('ARRAY'), or both ('BOTH'). | 'BOTH' |
LATERAL | LATERAL is an optional keyword used to reference columns defined to the left of the LATERAL keyword within the FROM clause. LATERAL enables cross-referencing between the preceding table expressions and the function. | - |
Output
The following table describes the output columns of the FLATTEN function:
Note: When using the LATERAL keyword with FLATTEN, these output columns may not be explicitly provided, as LATERAL introduces dynamic cross-referencing, altering the output structure.
Column | Description |
---|
SEQ | A unique sequence number associated with the input. |
KEY | Key to the expanded value. If the flattened element does not contain a key, it's set to NULL. |
PATH | Path to the flattened element. |
INDEX | If the element is an array, this column contains its index; otherwise, it's set to NULL. |
VALUE | Value of the flattened element. |
THIS | This column identifies the element currently being flattened. |
SQL Examples
SQL Examples 1: Demonstrating PATH, OUTER, RECURSIVE, and MODE Parameters
This example demonstrates the behavior of the FLATTEN function with respect to the PATH, OUTER, RECURSIVE, and MODE parameters.
SELECT
*
FROM
FLATTEN (
INPUT => PARSE_JSON (
'{"name": "John", "languages": ["English", "Spanish", "French"], "address": {"city": "New York", "state": "NY"}}'
)
);
┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ seq │ key │ path │ index │ value │ this │
├────────┼──────────────────┼──────────────────┼──────────────────┼──────────────────────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ 1 │ address │ address │ NULL │ {"city":"New York","state":"NY"} │ {"address":{"city":"New York","state":"NY"},"languages":["English","Spanish","French"],"name":"John"} │
│ 1 │ languages │ languages │ NULL │ ["English","Spanish","French"] │ {"address":{"city":"New York","state":"NY"},"languages":["English","Spanish","French"],"name":"John"} │
│ 1 │ name │ name │ NULL │ "John" │ {"address":{"city":"New York","state":"NY"},"languages":["English","Spanish","French"],"name":"John"} │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
-- PATH helps in selecting elements at a specific path from the original JSON data.
SELECT
*
FROM
FLATTEN (
INPUT => PARSE_JSON (
'{"name": "John", "languages": ["English", "Spanish", "French"], "address": {"city": "New York", "state": "NY"}}'
),
PATH => 'languages'
);
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ seq │ key │ path │ index │ value │ this │
├────────┼──────────────────┼──────────────────┼──────────────────┼───────────────────┼────────────────────────────────┤
│ 1 │ NULL │ languages[0] │ 0 │ "English" │ ["English","Spanish","French"] │
│ 1 │ NULL │ languages[1] │ 1 │ "Spanish" │ ["English","Spanish","French"] │
│ 1 │ NULL │ languages[2] │ 2 │ "French" │ ["English","Spanish","French"] │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
-- RECURSIVE enables recursive flattening of nested structures.
SELECT
*
FROM
FLATTEN (
INPUT => PARSE_JSON (
'{"name": "John", "languages": ["English", "Spanish", "French"], "address": {"city": "New York", "state": "NY"}}'
),
RECURSIVE => TRUE
);
┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ seq │ key │ path │ index │ value │ this │
├────────┼──────────────────┼──────────────────┼──────────────────┼──────────────────────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ 1 │ address │ address │ NULL │ {"city":"New York","state":"NY"} │ {"address":{"city":"New York","state":"NY"},"languages":["English","Spanish","French"],"name":"John"} │
│ 1 │ city │ address.city │ NULL │ "New York" │ {"city":"New York","state":"NY"} │
│ 1 │ state │ address.state │ NULL │ "NY" │ {"city":"New York","state":"NY"} │
│ 1 │ languages │ languages │ NULL │ ["English","Spanish","French"] │ {"address":{"city":"New York","state":"NY"},"languages":["English","Spanish","French"],"name":"John"} │
│ 1 │ NULL │ languages[0] │ 0 │ "English" │ ["English","Spanish","French"] │
│ 1 │ NULL │ languages[1] │ 1 │ "Spanish" │ ["English","Spanish","French"] │
│ 1 │ NULL │ languages[2] │ 2 │ "French" │ ["English","Spanish","French"] │
│ 1 │ name │ name │ NULL │ "John" │ {"address":{"city":"New York","state":"NY"},"languages":["English","Spanish","French"],"name":"John"} │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
-- MODE specifies whether only objects ('OBJECT'), only arrays ('ARRAY'), or both ('BOTH') should be flattened.
-- In this example, MODE => 'ARRAY' is used, which means that only arrays within the JSON data will be flattened.
SELECT
*
FROM
FLATTEN (
INPUT => PARSE_JSON (
'{"name": "John", "languages": ["English", "Spanish", "French"], "address": {"city": "New York", "state": "NY"}}'
),
MODE => 'ARRAY'
);
---
-- OUTER determines the inclusion of zero-row expansions in the output.
-- In this first example, OUTER => TRUE is used with an empty JSON array, which results in zero-row expansions.
-- Rows are included in the output even when there are no values to flatten.
SELECT
*
FROM
FLATTEN (INPUT => PARSE_JSON ('[]'), OUTER => TRUE);
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ seq │ key │ path │ index │ value │ this │
├────────┼──────────────────┼──────────────────┼──────────────────┼───────────────────┼───────────────────┤
│ 1 │ NULL │ NULL │ NULL │ NULL │ NULL │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────┘
-- In this second example, OUTER is omitted, and the output shows how rows with zero results are not included when OUTER is not specified.
SELECT
*
FROM
FLATTEN (INPUT => PARSE_JSON ('[]'));
SQL Examples 2: Demonstrating LATERAL FLATTEN
This example demonstrates the behavior of the FLATTEN function when used in conjunction with the LATERAL keyword.
-- Create a table for Tim Hortons transactions with multiple items
CREATE TABLE tim_hortons_transactions (
transaction_id INT,
customer_id INT,
items VARIANT
);
-- Insert data for Tim Hortons transactions with multiple items
INSERT INTO tim_hortons_transactions (transaction_id, customer_id, items)
VALUES
(101, 1, parse_json('[{"item":"coffee", "price":2.50}, {"item":"donut", "price":1.20}]')),
(102, 2, parse_json('[{"item":"bagel", "price":1.80}, {"item":"muffin", "price":2.00}]')),
(103, 3, parse_json('[{"item":"timbit_assortment", "price":5.00}]'));
-- Show Tim Hortons transactions with multiple items using LATERAL FLATTEN
SELECT
t.transaction_id,
t.customer_id,
f.value:item::STRING AS purchased_item,
f.value:price::FLOAT AS price
FROM
tim_hortons_transactions t,
LATERAL FLATTEN(input => t.items) f;
┌───────────────────────────────────────────────────────────────────────────┐
│ transaction_id │ customer_id │ purchased_item │ price │
├─────────────────┼─────────────────┼───────────────────┼───────────────────┤
│ 101 │ 1 │ coffee │ 2.5 │
│ 101 │ 1 │ donut │ 1.2 │
│ 102 │ 2 │ bagel │ 1.8 │
│ 102 │ 2 │ muffin │ 2 │
│ 103 │ 3 │ timbit_assortment │ 5 │
└───────────────────────────────────────────────────────────────────────────┘
-- Find maximum, minimum, and average prices of the purchased items
SELECT
MAX(f.value:price::FLOAT) AS max_price,
MIN(f.value:price::FLOAT) AS min_price,
AVG(f.value:price::FLOAT) AS avg_price
FROM
tim_hortons_transactions t,
LATERAL FLATTEN(input => t.items) f;
┌───────────────────────────────────────────────────────────┐
│ max_price │ min_price │ avg_price │
├───────────────────┼───────────────────┼───────────────────┤
│ 5 │ 1.2 │ 2.5 │
└───────────────────────────────────────────────────────────┘
4 - GET
Extracts value from a Variant
that contains ARRAY
by index
, or a Variant
that contains OBJECT
by field_name
.
The value is returned as a Variant
or NULL
if either of the arguments is NULL
.
GET
applies case-sensitive matching to field_name
. For case-insensitive matching, use GET_IGNORE_CASE
.
Analyze Syntax
func.get(<variant>, <index>)
or
func.get(<variant>, <field_name>)
Analyze Example
func.get(func.parse_json('[2.71, 3.14]'), 0);
+----------------------------------------------+
| func.get(func.parse_json('[2.71, 3.14]'), 0) |
+----------------------------------------------+
| 2.71 |
+----------------------------------------------+
func.get(func.parse_json('{"aa":1, "aA":2, "Aa":3}'), 'aa');
+-------------------------------------------------------------+
| func.get(func.parse_json('{"aa":1, "aA":2, "Aa":3}'), 'aa') |
+-------------------------------------------------------------+
| 1 |
+-------------------------------------------------------------+
SQL Syntax
GET( <variant>, <index> )
GET( <variant>, <field_name> )
Arguments
Arguments | Description |
---|
<variant> | The VARIANT value that contains either an ARRAY or an OBJECT |
<index> | The Uint32 value specifies the position of the value in ARRAY |
<field_name> | The String value specifies the key in a key-value pair of OBJECT |
Return Type
VARIANT
SQL Examples
SELECT get(parse_json('[2.71, 3.14]'), 0);
+------------------------------------+
| get(parse_json('[2.71, 3.14]'), 0) |
+------------------------------------+
| 2.71 |
+------------------------------------+
SELECT get(parse_json('{"aa":1, "aA":2, "Aa":3}'), 'aa');
+---------------------------------------------------+
| get(parse_json('{"aa":1, "aA":2, "Aa":3}'), 'aa') |
+---------------------------------------------------+
| 1 |
+---------------------------------------------------+
SELECT get(parse_json('{"aa":1, "aA":2, "Aa":3}'), 'AA');
+---------------------------------------------------+
| get(parse_json('{"aa":1, "aA":2, "Aa":3}'), 'AA') |
+---------------------------------------------------+
| NULL |
+---------------------------------------------------+
5 - GET_IGNORE_CASE
Extracts value from a VARIANT
that contains OBJECT
by the field_name.
The value is returned as a Variant
or NULL
if either of the arguments is NULL
.
GET_IGNORE_CASE
is similar to GET
but applies case-insensitive matching to field names.
First match the exact same field name, if not found, match the case-insensitive field name alphabetically.
Analyze Syntax
func.get_ignore_Case(<variant>, <field_name>)
Analyze Example
func.get_ignore_case(func.parse_json('{"aa":1, "aA":2, "Aa":3}'), 'AA')
+-------------------------------------------------------------------------+
| func.get_ignore_case(func.parse_json('{"aa":1, "aA":2, "Aa":3}'), 'AA') |
+-------------------------------------------------------------------------+
| 3 |
+-------------------------------------------------------------------------+
SQL Syntax
GET_IGNORE_CASE( <variant>, <field_name> )
Arguments
Arguments | Description |
---|
<variant> | The VARIANT value that contains either an ARRAY or an OBJECT |
<field_name> | The String value specifies the key in a key-value pair of OBJECT |
Return Type
VARIANT
SQL Examples
SELECT get_ignore_case(parse_json('{"aa":1, "aA":2, "Aa":3}'), 'AA');
+---------------------------------------------------------------+
| get_ignore_case(parse_json('{"aa":1, "aA":2, "Aa":3}'), 'AA') |
+---------------------------------------------------------------+
| 3 |
+---------------------------------------------------------------+
6 - GET_PATH
Extracts value from a VARIANT
by path_name
.
The value is returned as a Variant
or NULL
if either of the arguments is NULL
.
GET_PATH
is equivalent to a chain of GET
functions, path_name
consists of a concatenation of field names preceded by periods (.), colons (:) or index operators ([index]
). The first field name does not require the leading identifier to be specified.
Analyze Syntax
func.get_path(<variant>, <path_name>)
Analyze Example
func.get_path(func.parse_json('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}'), 'k2.k4')
+---------------------------------------------------------------------------------+
| func.get_path(func.parse_json('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}'), 'k2.k4') |
+---------------------------------------------------------------------------------+
| 4 |
+---------------------------------------------------------------------------------+
SQL Syntax
GET_PATH( <variant>, <path_name> )
Arguments
Arguments | Description |
---|
<variant> | The VARIANT value that contains either an ARRAY or an OBJECT |
<path_name> | The String value that consists of a concatenation of field names |
Return Type
VARIANT
SQL Examples
SELECT get_path(parse_json('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}'), 'k1[0]');
+-----------------------------------------------------------------------+
| get_path(parse_json('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}'), 'k1[0]') |
+-----------------------------------------------------------------------+
| 0 |
+-----------------------------------------------------------------------+
SELECT get_path(parse_json('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}'), 'k2:k3');
+-----------------------------------------------------------------------+
| get_path(parse_json('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}'), 'k2:k3') |
+-----------------------------------------------------------------------+
| 3 |
+-----------------------------------------------------------------------+
SELECT get_path(parse_json('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}'), 'k2.k4');
+-----------------------------------------------------------------------+
| get_path(parse_json('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}'), 'k2.k4') |
+-----------------------------------------------------------------------+
| 4 |
+-----------------------------------------------------------------------+
SELECT get_path(parse_json('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}'), 'k2.k5');
+-----------------------------------------------------------------------+
| get_path(parse_json('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}'), 'k2.k5') |
+-----------------------------------------------------------------------+
| NULL |
+-----------------------------------------------------------------------+
7 - IS_ARRAY
Checks if the input value is a JSON array. Please note that a JSON array is not the same as the ARRAY data type. A JSON array is a data structure commonly used in JSON, representing an ordered collection of values enclosed within square brackets [ ]
. It is a flexible format for organizing and exchanging various data types, including strings, numbers, booleans, objects, and nulls.
[
"Apple",
42,
true,
{"name": "John", "age": 30, "isStudent": false},
[1, 2, 3],
null
]
Analyze Syntax
Analyze Example
func.is_array(func.parse_json('true')), func.is_array(func.parse_json('[1,2,3]'))
┌────────────────────────────────────────────────────────────────────────────────────┐
│ func.is_array(func.parse_json('true')) │ func.is_array(func.parse_json('[1,2,3]')) │
├────────────────────────────────────────┼───────────────────────────────────────────┤
│ false │ true │
└────────────────────────────────────────────────────────────────────────────────────┘
SQL Syntax
Return Type
Returns true
if the input value is a JSON array, and false
otherwise.
SQL Examples
SELECT
IS_ARRAY(PARSE_JSON('true')),
IS_ARRAY(PARSE_JSON('[1,2,3]'));
┌────────────────────────────────────────────────────────────────┐
│ is_array(parse_json('true')) │ is_array(parse_json('[1,2,3]')) │
├──────────────────────────────┼─────────────────────────────────┤
│ false │ true │
└────────────────────────────────────────────────────────────────┘
8 - IS_BOOLEAN
Checks if the input JSON value is a boolean.
Analyze Syntax
Analyze Example
func.is_boolean(func.parse_json('true')), func.is_boolean(func.parse_json('[1,2,3]'))
┌────────────────────────────────────────────────────────────────────────────────────────┐
│ func.is_boolean(func.parse_json('true')) │ func.is_boolean(func.parse_json('[1,2,3]')) │
├──────────────────────────────────────────┼─────────────────────────────────────────────┤
│ true │ false │
└────────────────────────────────────────────────────────────────────────────────────────┘
SQL Syntax
Return Type
Returns true
if the input JSON value is a boolean, and false
otherwise.
SQL Examples
SELECT
IS_BOOLEAN(PARSE_JSON('true')),
IS_BOOLEAN(PARSE_JSON('[1,2,3]'));
┌────────────────────────────────────────────────────────────────────┐
│ is_boolean(parse_json('true')) │ is_boolean(parse_json('[1,2,3]')) │
├────────────────────────────────┼───────────────────────────────────┤
│ true │ false │
└────────────────────────────────────────────────────────────────────┘
9 - IS_FLOAT
Checks if the input JSON value is a float.
Analyze Syntax
Analyze Example
func.is_float(func.parse_json('1.23')), func.is_float(func.parse_json('[1,2,3]'))
┌────────────────────────────────────────────────────────────────────────────────────────┐
│ func.is_float(func.parse_json('1.23')) │ func.is_float(func.parse_json('[1,2,3]')) │
├──────────────────────────────────────────┼─────────────────────────────────────────────┤
│ true │ false │
└────────────────────────────────────────────────────────────────────────────────────────┘
SQL Syntax
Return Type
Returns true
if the input JSON value is a float, and false
otherwise.
SQL Examples
SELECT
IS_FLOAT(PARSE_JSON('1.23')),
IS_FLOAT(PARSE_JSON('[1,2,3]'));
┌────────────────────────────────────────────────────────────────┐
│ is_float(parse_json('1.23')) │ is_float(parse_json('[1,2,3]')) │
├──────────────────────────────┼─────────────────────────────────┤
│ true │ false │
└────────────────────────────────────────────────────────────────┘
10 - IS_INTEGER
Checks if the input JSON value is an integer.
Analyze Syntax
Analyze Example
func.is_integer(func.parse_json('123')), func.is_integer(func.parse_json('[1,2,3]'))
┌────────────────────────────────────────────────────────────────────────────────────────┐
│ func.is_integer(func.parse_json('123')) │ func.is_integer(func.parse_json('[1,2,3]')) │
├──────────────────────────────────────────┼─────────────────────────────────────────────┤
│ true │ false │
└────────────────────────────────────────────────────────────────────────────────────────┘
SQL Syntax
Return Type
Returns true
if the input JSON value is an integer, and false
otherwise.
SQL Examples
SELECT
IS_INTEGER(PARSE_JSON('123')),
IS_INTEGER(PARSE_JSON('[1,2,3]'));
┌───────────────────────────────────────────────────────────────────┐
│ is_integer(parse_json('123')) │ is_integer(parse_json('[1,2,3]')) │
├───────────────────────────────┼───────────────────────────────────┤
│ true │ false │
└───────────────────────────────────────────────────────────────────┘
11 - IS_NULL_VALUE
import FunctionDescription from '@site/src/components/FunctionDescription';
Checks whether the input value is a JSON null
. Please note that this function examines JSON null
, not SQL NULL. To check if a value is SQL NULL, use IS_NULL.
{
"name": "John",
"age": null
}
Analyze Syntax
func.is_null_value(<expr>)
Analyze Example
func.is_null_value(func.get_path(func.parse_json('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}'), 'k2.k5'))
┌─────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ func.is_null_value(func.get_path(func.parse_json('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}'), 'k2.k5')) │
├─────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ true │
└─────────────────────────────────────────────────────────────────────────────────────────────────────┘
SQL Syntax
Return Type
Returns true
if the input value is a JSON null
, and false
otherwise.
SQL Examples
SELECT
IS_NULL_VALUE(PARSE_JSON('{"name":"John", "age":null}') :age), --JSON null
IS_NULL(NULL); --SQL NULL
┌──────────────────────────────────────────────────────────────────────────────┐
│ is_null_value(parse_json('{"name":"john", "age":null}'):age) │ is_null(null) │
├──────────────────────────────────────────────────────────────┼───────────────┤
│ true │ true │
└──────────────────────────────────────────────────────────────────────────────┘
12 - IS_OBJECT
Checks if the input value is a JSON object.
Analyze Syntax
Analyze Example
func.is_object(func.parse_json('{"a":"b"}')), func.is_object(func.parse_json('["a","b","c"]'))
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ func.is_object(func.parse_json('{"a":"b"}')) │ func.is_object(func.parse_json('["a","b","c"]')) │
├───────────────────────────────────────────────┼──────────────────────────────────────────────────┤
│ true │ false │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
SQL Syntax
Return Type
Returns true
if the input JSON value is a JSON object, and false
otherwise.
SQL Examples
SELECT
IS_OBJECT(PARSE_JSON('{"a":"b"}')), -- JSON Object
IS_OBJECT(PARSE_JSON('["a","b","c"]')); --JSON Array
┌─────────────────────────────────────────────────────────────────────────────┐
│ is_object(parse_json('{"a":"b"}')) │ is_object(parse_json('["a","b","c"]')) │
├────────────────────────────────────┼────────────────────────────────────────┤
│ true │ false │
└─────────────────────────────────────────────────────────────────────────────┘
13 - IS_STRING
Checks if the input JSON value is a string.
Analyze Syntax
Analyze Example
func.is_string(func.parse_json('"abc"')), func.is_string(func.parse_json('123'))
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ func.is_string(func.parse_json('"abc"')) │ func.is_string(func.parse_json('123')) │
├───────────────────────────────────────────────┼──────────────────────────────────────────────────┤
│ true │ false │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
SQL Syntax
Return Type
Returns true
if the input JSON value is a string, and false
otherwise.
SQL Examples
SELECT
IS_STRING(PARSE_JSON('"abc"')),
IS_STRING(PARSE_JSON('123'));
┌───────────────────────────────────────────────────────────────┐
│ is_string(parse_json('"abc"')) │ is_string(parse_json('123')) │
├────────────────────────────────┼──────────────────────────────┤
│ true │ false │
└───────────────────────────────────────────────────────────────┘
14 - JSON_ARRAY
Creates a JSON array with specified values.
Analyze Syntax
func.json_array(value1[, value2[, ...]])
Analyze Example
func.json_array('fruits', func.json_array('apple', 'banana', 'orange'), func.json_object('price', 1.2, 'quantity', 3)) |
-----------------------------------------------------------------------------------------------------------------------+
["fruits",["apple","banana","orange"],{"price":1.2,"quantity":3}] |
SQL Syntax
JSON_ARRAY(value1[, value2[, ...]])
Return Type
JSON array.
SQL Examples
SQL Examples 1: Creating JSON Array with Constant Values or Expressions
SELECT JSON_ARRAY('PlaidCloud Lakehouse', 3.14, NOW(), TRUE, NULL);
json_array('databend', 3.14, now(), true, null) |
--------------------------------------------------------+
["PlaidCloud Lakehouse",3.14,"2023-09-06 07:23:55.399070",true,null]|
SELECT JSON_ARRAY('fruits', JSON_ARRAY('apple', 'banana', 'orange'), JSON_OBJECT('price', 1.2, 'quantity', 3));
json_array('fruits', json_array('apple', 'banana', 'orange'), json_object('price', 1.2, 'quantity', 3))|
-------------------------------------------------------------------------------------------------------+
["fruits",["apple","banana","orange"],{"price":1.2,"quantity":3}] |
SQL Examples 2: Creating JSON Array from Table Data
CREATE TABLE products (
ProductName VARCHAR(255),
Price DECIMAL(10, 2)
);
INSERT INTO products (ProductName, Price)
VALUES
('Apple', 1.2),
('Banana', 0.5),
('Orange', 0.8);
SELECT JSON_ARRAY(ProductName, Price) FROM products;
json_array(productname, price)|
------------------------------+
["Apple",1.2] |
["Banana",0.5] |
["Orange",0.8] |
15 - JSON_ARRAY_ELEMENTS
Extracts the elements from a JSON array, returning them as individual rows in the result set. JSON_ARRAY_ELEMENTS does not recursively expand nested arrays; it treats them as single elements.
Analyze Syntax
func.json_array_elements(<json_string>)
Analyze Example
func.json_array_elements(func.parse_json('[ \n {"product": "laptop", "brand": "apple", "price": 1500},\n {"product": "smartphone", "brand": "samsung", "price": 800},\n {"product": "headphones", "brand": "sony", "price": 150}\n]'))
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ func.json_array_elements(func.parse_json('[ \n {"product": "laptop", "brand": "apple", "price": 1500},\n {"product": "smartphone", "brand": "samsung", "price": 800},\n {"product": "headphones", "brand": "sony", "price": 150}\n]')) │
├───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ {"brand":"Apple","price":1500,"product":"Laptop"} │
│ {"brand":"Samsung","price":800,"product":"Smartphone"} │
│ {"brand":"Sony","price":150,"product":"Headphones"} │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
SQL Syntax
JSON_ARRAY_ELEMENTS(<json_string>)
Return Type
JSON_ARRAY_ELEMENTS returns a set of VARIANT values, each representing an element extracted from the input JSON array.
SQL Examples
-- Extract individual elements from a JSON array containing product information
SELECT
JSON_ARRAY_ELEMENTS(
PARSE_JSON (
'[
{"product": "Laptop", "brand": "Apple", "price": 1500},
{"product": "Smartphone", "brand": "Samsung", "price": 800},
{"product": "Headphones", "brand": "Sony", "price": 150}
]'
)
);
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ json_array_elements(parse_json('[ \n {"product": "laptop", "brand": "apple", "price": 1500},\n {"product": "smartphone", "brand": "samsung", "price": 800},\n {"product": "headphones", "brand": "sony", "price": 150}\n]')) │
├─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ {"brand":"Apple","price":1500,"product":"Laptop"} │
│ {"brand":"Samsung","price":800,"product":"Smartphone"} │
│ {"brand":"Sony","price":150,"product":"Headphones"} │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
-- Display data types of the extracted elements
SELECT
TYPEOF (
JSON_ARRAY_ELEMENTS(
PARSE_JSON (
'[
{"product": "Laptop", "brand": "Apple", "price": 1500},
{"product": "Smartphone", "brand": "Samsung", "price": 800},
{"product": "Headphones", "brand": "Sony", "price": 150}
]'
)
)
);
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ typeof(json_array_elements(parse_json('[ \n {"product": "laptop", "brand": "apple", "price": 1500},\n {"product": "smartphone", "brand": "samsung", "price": 800},\n {"product": "headphones", "brand": "sony", "price": 150}\n]'))) │
├─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ VARIANT NULL │
│ VARIANT NULL │
│ VARIANT NULL │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
16 - JSON_EACH
Extracts key-value pairs from a JSON object, breaking down the structure into individual rows in the result set. Each row represents a distinct key-value pair derived from the input JSON expression.
Analyze Syntax
func.json_each(<json_string>)
Analyze Example
┌────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ func.json_each(func.parse_json('{"name": "john", "age": 25, "isstudent": false, "grades": [90, 85, 92]}')) │
├────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ('age','25') │
│ ('grades','[90,85,92]') │
│ ('isStudent','false') │
│ ('name','"John"') │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
SQL Syntax
Return Type
JSON_EACH returns a set of tuples, each consisting of a STRING key and a corresponding VARIANT value.
SQL Examples
-- Extract key-value pairs from a JSON object representing information about a person
SELECT
JSON_EACH(
PARSE_JSON (
'{"name": "John", "age": 25, "isStudent": false, "grades": [90, 85, 92]}'
)
);
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ json_each(parse_json('{"name": "john", "age": 25, "isstudent": false, "grades": [90, 85, 92]}')) │
├──────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ('age','25') │
│ ('grades','[90,85,92]') │
│ ('isStudent','false') │
│ ('name','"John"') │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
-- Display data types of the extracted values
SELECT
TYPEOF (
JSON_EACH(
PARSE_JSON (
'{"name": "John", "age": 25, "isStudent": false, "grades": [90, 85, 92]}'
)
)
);
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ typeof(json_each(parse_json('{"name": "john", "age": 25, "isstudent": false, "grades": [90, 85, 92]}'))) │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ TUPLE(STRING, VARIANT) NULL │
│ TUPLE(STRING, VARIANT) NULL │
│ TUPLE(STRING, VARIANT) NULL │
│ TUPLE(STRING, VARIANT) NULL │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────┘
17 - JSON_EXTRACT_PATH_TEXT
Extracts value from a Json string by path_name
.
The value is returned as a String
or NULL
if either of the arguments is NULL
.
This function is equivalent to to_varchar(GET_PATH(PARSE_JSON(JSON), PATH_NAME))
.
Analyze Syntax
func.json_extract_path_text(<expr>, <path_name>)
Analyze Example
func.json_extract_path_text('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}', 'k2.k4')
+------------------------------------------------------------------------------+
| func.json_extract_path_text('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}', 'k2.k4') |
+------------------------------------------------------------------------------+
| 4 |
+------------------------------------------------------------------------------+
SQL Syntax
JSON_EXTRACT_PATH_TEXT( <expr>, <path_name> )
Arguments
Arguments | Description |
---|
<expr> | The Json String value |
<path_name> | The String value that consists of a concatenation of field names |
Return Type
String
SQL Examples
SELECT json_extract_path_text('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}', 'k1[0]');
+-------------------------------------------------------------------------+
| json_extract_path_text('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}', 'k1[0]') |
+-------------------------------------------------------------------------+
| 0 |
+-------------------------------------------------------------------------+
SELECT json_extract_path_text('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}', 'k2:k3');
+-------------------------------------------------------------------------+
| json_extract_path_text('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}', 'k2:k3') |
+-------------------------------------------------------------------------+
| 3 |
+-------------------------------------------------------------------------+
SELECT json_extract_path_text('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}', 'k2.k4');
+-------------------------------------------------------------------------+
| json_extract_path_text('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}', 'k2.k4') |
+-------------------------------------------------------------------------+
| 4 |
+-------------------------------------------------------------------------+
SELECT json_extract_path_text('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}', 'k2.k5');
+-------------------------------------------------------------------------+
| json_extract_path_text('{"k1":[0,1,2], "k2":{"k3":3,"k4":4}}', 'k2.k5') |
+-------------------------------------------------------------------------+
| NULL |
+-------------------------------------------------------------------------+
18 - JSON_OBJECT_KEYS
Returns an Array containing the list of keys in the input Variant OBJECT.
Analyze Syntax
func.json_object_keys(<variant>)
Analyze Example
func.json_object_keys(func.parse_json(parse_json('{"a": 1, "b": [1,2,3]}')), func.json_object_keys(func.parse_json(parse_json('{"b": [2,3,4]}'))
┌─────────────────────────────────────────────────────────────────┐
│ id │ json_object_keys(var) │ json_object_keys(var) │
├────────────────┼────────────────────────┼───────────────────────┤
│ 1 │ ["a","b"] │ ["a","b"] │
│ 2 │ ["b"] │ ["b"] │
└─────────────────────────────────────────────────────────────────┘
SQL Syntax
JSON_OBJECT_KEYS(<variant>)
Arguments
Arguments | Description |
---|
<variant> | The VARIANT value that contains an OBJECT |
Aliases
Return Type
Array<String>
SQL Examples
CREATE TABLE IF NOT EXISTS objects_test1(id TINYINT, var VARIANT);
INSERT INTO
objects_test1
VALUES
(1, parse_json('{"a": 1, "b": [1,2,3]}'));
INSERT INTO
objects_test1
VALUES
(2, parse_json('{"b": [2,3,4]}'));
SELECT
id,
object_keys(var),
json_object_keys(var)
FROM
objects_test1;
┌────────────────────────────────────────────────────────────┐
│ id │ object_keys(var) │ json_object_keys(var) │
├────────────────┼───────────────────┼───────────────────────┤
│ 1 │ ["a","b"] │ ["a","b"] │
│ 2 │ ["b"] │ ["b"] │
└────────────────────────────────────────────────────────────┘
19 - JSON_PATH_EXISTS
Checks whether a specified path exists in JSON data.
Analyze Syntax
func.json_path_exists(<json_data>, <json_path_expression)
Analyze Example
func.json_path_exists(parse_json('{"a": 1, "b": 2}'), '$.a ? (@ == 1)'), func.json_path_exists(parse_json('{"a": 1, "b": 2}'), '$.a ? (@ > 1)')
┌─────────────────────────────┐
│ Item 1 │ Item 2 │
├────────────────┼────────────┤
│ True │ False │
└─────────────────────────────┘
SQL Syntax
JSON_PATH_EXISTS(<json_data>, <json_path_expression>)
json_data: Specifies the JSON data you want to search within. It can be a JSON object or an array.
json_path_expression: Specifies the path, starting from the root of the JSON data represented by $
, that you want to check within the JSON data. You can also include conditions within the expression, using @
to refer to the current node or element being evaluated, to filter the results.
Return Type
The function returns:
true
if the specified JSON path (and conditions if any) exists within the JSON data.false
if the specified JSON path (and conditions if any) does not exist within the JSON data.- NULL if either the json_data or json_path_expression is NULL or invalid.
SQL Examples
SELECT JSON_PATH_EXISTS(parse_json('{"a": 1, "b": 2}'), '$.a ? (@ == 1)');
----
true
SELECT JSON_PATH_EXISTS(parse_json('{"a": 1, "b": 2}'), '$.a ? (@ > 1)');
----
false
SELECT JSON_PATH_EXISTS(NULL, '$.a');
----
NULL
SELECT JSON_PATH_EXISTS(parse_json('{"a": 1, "b": 2}'), NULL);
----
NULL
20 - JSON_PATH_MATCH
Checks whether a specified JSON path expression matches certain conditions within a JSON data. Please note that the @@
operator is synonymous with this function. For more information, see JSON Operators.
Analyze Syntax
func.json_path_match(<json_data>, <json_path_expression)
Analyze Example
func.json_path_match(func.parse_json('{"a":1,"b":[1,2,3]}'), '$.a == 1')
┌──────────────────────────────────────────────────────────────────────────┐
│ func.json_path_match(func.parse_json('{"a":1,"b":[1,2,3]}'), '$.a == 1') │
├──────────────────────────────────────────────────────────────────────────┤
│ true │
└──────────────────────────────────────────────────────────────────────────┘
func.json_path_match(func.parse_json('{"a":1,"b":[1,2,3]}'), '$.b[0] > 1')
┌────────────────────────────────────────────────────────────────────────────┐
│ func.json_path_match(func.parse_json('{"a":1,"b":[1,2,3]}'), '$.b[0] > 1') │
├────────────────────────────────────────────────────────────────────────────┤
│ false │
└────────────────────────────────────────────────────────────────────────────┘
SQL Syntax
JSON_PATH_MATCH(<json_data>, <json_path_expression>)
json_data
: Specifies the JSON data you want to examine. It can be a JSON object or an array.json_path_expression
: Specifies the conditions to be checked within the JSON data. This expression describes the specific path or criteria to be matched, such as verifying whether specific field values in the JSON structure meet certain conditions. The $
symbol represents the root of the JSON data. It is used to start the path expression and indicates the top-level object in the JSON structure.
Return Type
The function returns:
true
if the specified JSON path expression matches the conditions within the JSON data.false
if the specified JSON path expression does not match the conditions within the JSON data.- NULL if either
json_data
or json_path_expression
is NULL or invalid.
SQL Examples
-- Check if the value at JSON path $.a is equal to 1
SELECT JSON_PATH_MATCH(parse_json('{"a":1,"b":[1,2,3]}'), '$.a == 1');
┌────────────────────────────────────────────────────────────────┐
│ json_path_match(parse_json('{"a":1,"b":[1,2,3]}'), '$.a == 1') │
├────────────────────────────────────────────────────────────────┤
│ true │
└────────────────────────────────────────────────────────────────┘
-- Check if the first element in the array at JSON path $.b is greater than 1
SELECT JSON_PATH_MATCH(parse_json('{"a":1,"b":[1,2,3]}'), '$.b[0] > 1');
┌──────────────────────────────────────────────────────────────────┐
│ json_path_match(parse_json('{"a":1,"b":[1,2,3]}'), '$.b[0] > 1') │
├──────────────────────────────────────────────────────────────────┤
│ false │
└──────────────────────────────────────────────────────────────────┘
-- Check if any element in the array at JSON path $.b
-- from the second one to the last are greater than or equal to 2
SELECT JSON_PATH_MATCH(parse_json('{"a":1,"b":[1,2,3]}'), '$.b[1 to last] >= 2');
┌───────────────────────────────────────────────────────────────────────────┐
│ json_path_match(parse_json('{"a":1,"b":[1,2,3]}'), '$.b[1 to last] >= 2') │
├───────────────────────────────────────────────────────────────────────────┤
│ true │
└───────────────────────────────────────────────────────────────────────────┘
-- NULL is returned if either the json_data or json_path_expression is NULL or invalid.
SELECT JSON_PATH_MATCH(parse_json('{"a":1,"b":[1,2,3]}'), NULL);
┌──────────────────────────────────────────────────────────┐
│ json_path_match(parse_json('{"a":1,"b":[1,2,3]}'), null) │
├──────────────────────────────────────────────────────────┤
│ NULL │
└──────────────────────────────────────────────────────────┘
SELECT JSON_PATH_MATCH(NULL, '$.a == 1');
┌───────────────────────────────────┐
│ json_path_match(null, '$.a == 1') │
├───────────────────────────────────┤
│ NULL │
└───────────────────────────────────┘
21 - JSON_PATH_QUERY
Get all JSON items returned by JSON path for the specified JSON value.
Analyze Syntax
func.json_path_query(<variant>, <path_name>)
Analyze Example
table.name, func.json_path_query(table.details, '$.features.*').alias('all_features')
+------------+--------------+
| name | all_features |
+------------+--------------+
| Laptop | "16GB" |
| Laptop | "512GB" |
| Smartphone | "4GB" |
| Smartphone | "128GB" |
| Headphones | "20h" |
| Headphones | "5.0" |
+------------+--------------+
SQL Syntax
JSON_PATH_QUERY(<variant>, '<path_name>')
Return Type
VARIANT
SQL Examples
Create a Table and Insert Sample Data
CREATE TABLE products (
name VARCHAR,
details VARIANT
);
INSERT INTO products (name, details)
VALUES ('Laptop', '{"brand": "Dell", "colors": ["Black", "Silver"], "price": 1200, "features": {"ram": "16GB", "storage": "512GB"}}'),
('Smartphone', '{"brand": "Apple", "colors": ["White", "Black"], "price": 999, "features": {"ram": "4GB", "storage": "128GB"}}'),
('Headphones', '{"brand": "Sony", "colors": ["Black", "Blue", "Red"], "price": 150, "features": {"battery": "20h", "bluetooth": "5.0"}}');
Query Demo: Extracting All Features from Product Details
SELECT
name,
JSON_PATH_QUERY(details, '$.features.*') AS all_features
FROM
products;
Result
+------------+--------------+
| name | all_features |
+------------+--------------+
| Laptop | "16GB" |
| Laptop | "512GB" |
| Smartphone | "4GB" |
| Smartphone | "128GB" |
| Headphones | "20h" |
| Headphones | "5.0" |
+------------+--------------+
22 - JSON_PATH_QUERY_ARRAY
Get all JSON items returned by JSON path for the specified JSON value and wrap a result into an array.
Analyze Syntax
func.json_path_query_array(<variant>, <path_name>)
Analyze Example
table.name, func.json_path_query_array(table.details, '$.features.*').alias('all_features')
name | all_features
------------+-----------------------
Laptop | ["16GB", "512GB"]
Smartphone | ["4GB", "128GB"]
Headphones | ["20h", "5.0"]
SQL Syntax
JSON_PATH_QUERY_ARRAY(<variant>, '<path_name>')
Return Type
VARIANT
SQL Examples
Create a Table and Insert Sample Data
CREATE TABLE products (
name VARCHAR,
details VARIANT
);
INSERT INTO products (name, details)
VALUES ('Laptop', '{"brand": "Dell", "colors": ["Black", "Silver"], "price": 1200, "features": {"ram": "16GB", "storage": "512GB"}}'),
('Smartphone', '{"brand": "Apple", "colors": ["White", "Black"], "price": 999, "features": {"ram": "4GB", "storage": "128GB"}}'),
('Headphones', '{"brand": "Sony", "colors": ["Black", "Blue", "Red"], "price": 150, "features": {"battery": "20h", "bluetooth": "5.0"}}');
Query Demo: Extracting All Features from Product Details as an Array
SELECT
name,
JSON_PATH_QUERY_ARRAY(details, '$.features.*') AS all_features
FROM
products;
Result
name | all_features
-----------+-----------------------
Laptop | ["16GB", "512GB"]
Smartphone | ["4GB", "128GB"]
Headphones | ["20h", "5.0"]
23 - JSON_PATH_QUERY_FIRST
Get the first JSON item returned by JSON path for the specified JSON value.
Analyze Syntax
func.json_path_query_first(<variant>, <path_name>)
Analyze Example
table.name, func.json_path_query_first(table.details, '$.features.*').alias('first_feature')
+------------+---------------+
| name | first_feature |
+------------+---------------+
| Laptop | "16GB" |
| Laptop | "16GB" |
| Smartphone | "4GB" |
| Smartphone | "4GB" |
| Headphones | "20h" |
| Headphones | "20h" |
+------------+---------------+
SQL Syntax
JSON_PATH_QUERY_FIRST(<variant>, '<path_name>')
Return Type
VARIANT
SQL Examples
Create a Table and Insert Sample Data
CREATE TABLE products (
name VARCHAR,
details VARIANT
);
INSERT INTO products (name, details)
VALUES ('Laptop', '{"brand": "Dell", "colors": ["Black", "Silver"], "price": 1200, "features": {"ram": "16GB", "storage": "512GB"}}'),
('Smartphone', '{"brand": "Apple", "colors": ["White", "Black"], "price": 999, "features": {"ram": "4GB", "storage": "128GB"}}'),
('Headphones', '{"brand": "Sony", "colors": ["Black", "Blue", "Red"], "price": 150, "features": {"battery": "20h", "bluetooth": "5.0"}}');
Query Demo: Extracting the First Feature from Product Details
SELECT
name,
JSON_PATH_QUERY(details, '$.features.*') AS all_features,
JSON_PATH_QUERY_FIRST(details, '$.features.*') AS first_feature
FROM
products;
Result
+------------+--------------+---------------+
| name | all_features | first_feature |
+------------+--------------+---------------+
| Laptop | "16GB" | "16GB" |
| Laptop | "512GB" | "16GB" |
| Smartphone | "4GB" | "4GB" |
| Smartphone | "128GB" | "4GB" |
| Headphones | "20h" | "20h" |
| Headphones | "5.0" | "20h" |
+------------+--------------+---------------+
24 - JSON_PRETTY
Formats JSON data, making it more readable and presentable. It automatically adds indentation, line breaks, and other formatting to the JSON data for better visual representation.
Analyze Syntax
func.json_pretty(<json_string>)
Analyze Example
func.json_pretty(func.parse_json('{"person": {"name": "bob", "age": 25}, "location": "city"}'))
┌─────────────────────────────────────────────────────────────────────────────────────────────────┐
│ func.json_pretty(func.parse_json('{"person": {"name": "bob", "age": 25}, "location": "city"}')) │
│ String │
├─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ { │
│ "location": "City", │
│ "person": { │
│ "age": 25, │
│ "name": "Bob" │
│ } │
│ } │
└─────────────────────────────────────────────────────────────────────────────────────────────────┘
SQL Syntax
JSON_PRETTY(<json_string>)
Return Type
String.
SQL Examples
SELECT JSON_PRETTY(PARSE_JSON('{"name":"Alice","age":30}'));
---
┌──────────────────────────────────────────────────────┐
│ json_pretty(parse_json('{"name":"alice","age":30}')) │
│ String │
├──────────────────────────────────────────────────────┤
│ { │
│ "age": 30, │
│ "name": "Alice" │
│ } │
└──────────────────────────────────────────────────────┘
SELECT JSON_PRETTY(PARSE_JSON('{"person": {"name": "Bob", "age": 25}, "location": "City"}'));
---
┌───────────────────────────────────────────────────────────────────────────────────────┐
│ json_pretty(parse_json('{"person": {"name": "bob", "age": 25}, "location": "city"}')) │
│ String │
├───────────────────────────────────────────────────────────────────────────────────────┤
│ { │
│ "location": "City", │
│ "person": { │
│ "age": 25, │
│ "name": "Bob" │
│ } │
│ } │
└───────────────────────────────────────────────────────────────────────────────────────┘
25 - JSON_STRIP_NULLS
Removes all properties with null values from a JSON object.
Analyze Syntax
func.json_strip_nulls(<json_string>)
Analyze Example
func.json_strip_nulls(func.parse_json('{"name": "alice", "age": 30, "city": null}'))
┌─────────────────────────────────────────────────────────────────────────────────────────────────┐
│ func.json_strip_nulls(func.parse_json('{"name": "alice", "age": 30, "city": null}')) │
│ String │
├─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ {"age":30,"name":"Alice"} │
└─────────────────────────────────────────────────────────────────────────────────────────────────┘
SQL Syntax
JSON_STRIP_NULLS(<json_string>)
Return Type
Returns a value of the same type as the input JSON value.
SQL Examples
SELECT JSON_STRIP_NULLS(PARSE_JSON('{"name": "Alice", "age": 30, "city": null}'));
json_strip_nulls(parse_json('{"name": "alice", "age": 30, "city": null}'))|
--------------------------------------------------------------------------+
{"age":30,"name":"Alice"} |
26 - JSON_TO_STRING
Alias for TO_STRING.
27 - JSON_TYPEOF
Returns the type of the main-level of a JSON structure.
Analyze Syntax
func.json_typeof(<json_string>)
Analyze Example
func.json_typeof(func.parse_json('null'))|
-----------------------------------------+
null |
--
func.json_typeof(func.parse_json('true'))|
-----------------------------------------+
boolean |
--
func.json_typeof(func.parse_json('"plaidcloud"'))|
-----------------------------------------------+
string |
--
func.json_typeof(func.parse_json('-1.23'))|
------------------------------------------+
number |
--
func.json_typeof(func.parse_json('[1,2,3]'))|
--------------------------------------------+
array |
--
func.json_typeof(func.parse_json('{"name": "alice", "age": 30}'))|
-----------------------------------------------------------------+
object |
SQL Syntax
JSON_TYPEOF(<json_string>)
Return Type
The return type of the json_typeof function (or similar) is a string that indicates the data type of the parsed JSON value. The possible return values are: 'null', 'boolean', 'string', 'number', 'array', and 'object'.
SQL Examples
-- Parsing a JSON value that is NULL
SELECT JSON_TYPEOF(PARSE_JSON(NULL));
--
func.json_typeof(func.parse_json(null))|
-----------------------------+
|
-- Parsing a JSON value that is the string 'null'
SELECT JSON_TYPEOF(PARSE_JSON('null'));
--
func.json_typeof(func.parse_json('null'))|
-------------------------------+
null |
SELECT JSON_TYPEOF(PARSE_JSON('true'));
--
func.json_typeof(func.parse_json('true'))|
-------------------------------+
boolean |
SELECT JSON_TYPEOF(PARSE_JSON('"PlaidCloud Lakehouse"'));
--
func.json_typeof(func.parse_json('"databend"'))|
-------------------------------------+
string |
SELECT JSON_TYPEOF(PARSE_JSON('-1.23'));
--
func.json_typeof(func.parse_json('-1.23'))|
--------------------------------+
number |
SELECT JSON_TYPEOF(PARSE_JSON('[1,2,3]'));
--
func.json_typeof(func.parse_json('[1,2,3]'))|
----------------------------------+
array |
SELECT JSON_TYPEOF(PARSE_JSON('{"name": "Alice", "age": 30}'));
--
func.json_typeof(func.parse_json('{"name": "alice", "age": 30}'))|
-------------------------------------------------------+
object |
28 - OBJECT_KEYS
Alias for JSON_OBJECT_KEYS.
29 - PARSE_JSON
Interprets input JSON string, producing a VARIANT value
parse_json
and try_parse_json
interprets an input string as a JSON document, producing a VARIANT value.
try_parse_json
returns a NULL value if an error occurs during parsing.
Analyze Syntax
func.parse_json(<json_string>)
or
func.try_parse_json(<json_string>)
Analyze Example
func.parse_json('[-1, 12, 289, 2188, false]')
+-----------------------------------------------+
| func.parse_json('[-1, 12, 289, 2188, false]') |
+-----------------------------------------------+
| [-1,12,289,2188,false] |
+-----------------------------------------------+
func.try_parse_json('{ "x" : "abc", "y" : false, "z": 10} ')
+--------------------------------------------------------------+
| func.try_parse_json('{ "x" : "abc", "y" : false, "z": 10} ') |
+--------------------------------------------------------------+
| {"x":"abc","y":false,"z":10} |
+--------------------------------------------------------------+
SQL Syntax
PARSE_JSON(<expr>)
TRY_PARSE_JSON(<expr>)
Arguments
Arguments | Description |
---|
<expr> | An expression of string type (e.g. VARCHAR) that holds valid JSON information. |
Return Type
VARIANT
SQL Examples
SELECT parse_json('[-1, 12, 289, 2188, false]');
+------------------------------------------+
| parse_json('[-1, 12, 289, 2188, false]') |
+------------------------------------------+
| [-1,12,289,2188,false] |
+------------------------------------------+
SELECT try_parse_json('{ "x" : "abc", "y" : false, "z": 10} ');
+---------------------------------------------------------+
| try_parse_json('{ "x" : "abc", "y" : false, "z": 10} ') |
+---------------------------------------------------------+
| {"x":"abc","y":false,"z":10} |
+---------------------------------------------------------+