JSON Functions
There are two sets of functions to parse JSON:
simpleJSON*
(visitParam*
) which is made for parsing a limited subset of JSON extremely fast.JSONExtract*
which is made for parsing ordinary JSON.
simpleJSON (visitParam) functions
ClickHouse has special functions for working with simplified JSON. All these JSON functions are based on strong assumptions about what the JSON can be. They try to do as little as possible to get the job done as quickly as possible.
The following assumptions are made:
- The field name (function argument) must be a constant.
- The field name is somehow canonically encoded in JSON. For example:
simpleJSONHas('{"abc":"def"}', 'abc') = 1
, butsimpleJSONHas('{"\\u0061\\u0062\\u0063":"def"}', 'abc') = 0
- Fields are searched for on any nesting level, indiscriminately. If there are multiple matching fields, the first occurrence is used.
- The JSON does not have space characters outside of string literals.
simpleJSONHas
Checks whether there is a field named field_name
. The result is UInt8
.
Syntax
Alias: visitParamHas
.
Parameters
-
json
— The JSON in which the field is searched for. String -
field_name
— The name of the field to search for. String literal Returned value -
Returns
1
if the field exists,0
otherwise. UInt8.
Example
Query:
Result:
simpleJSONExtractUInt
Parses UInt64
from the value of the field named field_name
. If this is a string field, it tries to parse a number from the beginning of the string. If the field does not exist, or it exists but does not contain a number, it returns 0
.
Syntax
Alias: visitParamExtractUInt
.
Parameters
json
— The JSON in which the field is searched for. Stringfield_name
— The name of the field to search for. String literal
Returned value
- Returns the number parsed from the field if the field exists and contains a number,
0
otherwise. UInt64.
Example
Query:
Result:
simpleJSONExtractInt
Parses Int64
from the value of the field named field_name
. If this is a string field, it tries to parse a number from the beginning of the string. If the field does not exist, or it exists but does not contain a number, it returns 0
.
Syntax
Alias: visitParamExtractInt
.
Parameters
json
— The JSON in which the field is searched for. Stringfield_name
— The name of the field to search for. String literal
Returned value
- Returns the number parsed from the field if the field exists and contains a number,
0
otherwise. Int64.
Example
Query:
Result:
simpleJSONExtractFloat
Parses Float64
from the value of the field named field_name
. If this is a string field, it tries to parse a number from the beginning of the string. If the field does not exist, or it exists but does not contain a number, it returns 0
.
Syntax
Alias: visitParamExtractFloat
.
Parameters
json
— The JSON in which the field is searched for. Stringfield_name
— The name of the field to search for. String literal
Returned value
- Returns the number parsed from the field if the field exists and contains a number,
0
otherwise. Float64.
Example
Query:
Result:
simpleJSONExtractBool
Parses a true/false value from the value of the field named field_name
. The result is UInt8
.
Syntax
Alias: visitParamExtractBool
.
Parameters
json
— The JSON in which the field is searched for. Stringfield_name
— The name of the field to search for. String literal
Returned value
It returns 1
if the value of the field is true
, 0
otherwise. This means this function will return 0
including (and not only) in the following cases:
- If the field doesn't exists.
- If the field contains
true
as a string, e.g.:{"field":"true"}
. - If the field contains
1
as a numerical value.
Example
Query:
Result:
simpleJSONExtractRaw
Returns the value of the field named field_name
as a String
, including separators.
Syntax
Alias: visitParamExtractRaw
.
Parameters
json
— The JSON in which the field is searched for. Stringfield_name
— The name of the field to search for. String literal
Returned value
- Returns the value of the field as a string, including separators if the field exists, or an empty string otherwise.
String
Example
Query:
Result:
simpleJSONExtractString
Parses String
in double quotes from the value of the field named field_name
.
Syntax
Alias: visitParamExtractString
.
Parameters
json
— The JSON in which the field is searched for. Stringfield_name
— The name of the field to search for. String literal
Returned value
- Returns the unescaped value of a field as a string, including separators. An empty string is returned if the field doesn't contain a double quoted string, if unescaping fails or if the field doesn't exist. String.
Implementation details
There is currently no support for code points in the format \uXXXX\uYYYY
that are not from the basic multilingual plane (they are converted to CESU-8 instead of UTF-8).
Example
Query:
Result:
JSONExtract functions
The following functions are based on simdjson, and designed for more complex JSON parsing requirements.
isValidJSON
Checks that passed string is valid JSON.
Syntax
Examples
JSONHas
If the value exists in the JSON document, 1
will be returned. If the value does not exist, 0
will be returned.
Syntax
Parameters
json
— JSON string to parse. String.indices_or_keys
— A list of zero or more arguments, each of which can be either string or integer. String, Int*.
indices_or_keys
type:
- String = access object member by key.
- Positive integer = access the n-th member/key from the beginning.
- Negative integer = access the n-th member/key from the end.
Returned value
- Returns
1
if the value exists injson
, otherwise0
. UInt8.
Examples
Query:
The minimum index of the element is 1. Thus the element 0 does not exist. You may use integers to access both JSON arrays and JSON objects. For example:
JSONLength
Return the length of a JSON array or a JSON object. If the value does not exist or has the wrong type, 0
will be returned.
Syntax
Parameters
json
— JSON string to parse. String.indices_or_keys
— A list of zero or more arguments, each of which can be either string or integer. String, Int*.
indices_or_keys
type:
- String = access object member by key.
- Positive integer = access the n-th member/key from the beginning.
- Negative integer = access the n-th member/key from the end.
Returned value
- Returns the length of the JSON array or JSON object. Returns
0
if the value does not exist or has the wrong type. UInt64.
Examples
JSONType
Return the type of a JSON value. If the value does not exist, Null=0
will be returned (not usual Null, but Null=0
of Enum8('Null' = 0, 'String' = 34,...
). .
Syntax
Parameters
json
— JSON string to parse. String.indices_or_keys
— A list of zero or more arguments, each of which can be either string or integer. String, Int*.
indices_or_keys
type:
- String = access object member by key.
- Positive integer = access the n-th member/key from the beginning.
- Negative integer = access the n-th member/key from the end.
Returned value
- Returns the type of a JSON value as a string, otherwise if the value doesn't exists it returns
Null=0
. Enum.
Examples
JSONExtractUInt
Parses JSON and extracts a value of UInt type.
Syntax
Parameters
json
— JSON string to parse. String.indices_or_keys
— A list of zero or more arguments, each of which can be either string or integer. String, Int*.
indices_or_keys
type:
- String = access object member by key.
- Positive integer = access the n-th member/key from the beginning.
- Negative integer = access the n-th member/key from the end.
Returned value
- Returns a UInt value if it exists, otherwise it returns
0
. UInt64.
Examples
Query:
Result:
JSONExtractInt
Parses JSON and extracts a value of Int type.
Syntax
Parameters
json
— JSON string to parse. String.indices_or_keys
— A list of zero or more arguments, each of which can be either string or integer. String, Int*.
indices_or_keys
type:
- String = access object member by key.
- Positive integer = access the n-th member/key from the beginning.
- Negative integer = access the n-th member/key from the end.
Returned value
- Returns an Int value if it exists, otherwise it returns
0
. Int64.
Examples
Query:
Result:
JSONExtractFloat
Parses JSON and extracts a value of Int type.
Syntax
Parameters
json
— JSON string to parse. String.indices_or_keys
— A list of zero or more arguments, each of which can be either string or integer. String, Int*.
indices_or_keys
type:
- String = access object member by key.
- Positive integer = access the n-th member/key from the beginning.
- Negative integer = access the n-th member/key from the end.
Returned value
- Returns an Float value if it exists, otherwise it returns
0
. Float64.
Examples
Query:
Result:
JSONExtractBool
Parses JSON and extracts a boolean value. If the value does not exist or has a wrong type, 0
will be returned.
Syntax
Parameters
json
— JSON string to parse. String.indices_or_keys
— A list of zero or more arguments, each of which can be either string or integer. String, Int*.
indices_or_keys
type:
- String = access object member by key.
- Positive integer = access the n-th member/key from the beginning.
- Negative integer = access the n-th member/key from the end.
Returned value
- Returns a Boolean value if it exists, otherwise it returns
0
. Bool.
Example
Query:
Result:
JSONExtractString
Parses JSON and extracts a string. This function is similar to visitParamExtractString
functions. If the value does not exist or has a wrong type, an empty string will be returned.
Syntax
Parameters
json
— JSON string to parse. String.indices_or_keys
— A list of zero or more arguments, each of which can be either string or integer. String, Int*.
indices_or_keys
type:
- String = access object member by key.
- Positive integer = access the n-th member/key from the beginning.
- Negative integer = access the n-th member/key from the end.
Returned value
- Returns an unescaped string from
json
. If unescaping failed, if the value does not exist or if it has a wrong type then it returns an empty string. String.
Examples
JSONExtract
Parses JSON and extracts a value of the given ClickHouse data type. This function is a generalized version of the previous JSONExtract<type>
functions. Meaning:
JSONExtract(..., 'String')
returns exactly the same as JSONExtractString()
,
JSONExtract(..., 'Float64')
returns exactly the same as JSONExtractFloat()
.
Syntax
Parameters
json
— JSON string to parse. String.indices_or_keys
— A list of zero or more arguments, each of which can be either string or integer. String, Int*.return_type
— A string specifying the type of the value to extract. String.
indices_or_keys
type:
- String = access object member by key.
- Positive integer = access the n-th member/key from the beginning.
- Negative integer = access the n-th member/key from the end.
Returned value
- Returns a value if it exists of the specified return type, otherwise it returns
0
,Null
, or an empty-string depending on the specified return type. UInt64, Int64, Float64, Bool or String.
Examples
Referring to a nested values by passing multiple indices_or_keys parameters:
Result:
JSONExtractKeysAndValues
Parses key-value pairs from JSON where the values are of the given ClickHouse data type.
Syntax
Parameters
json
— JSON string to parse. String.indices_or_keys
— A list of zero or more arguments, each of which can be either string or integer. String, Int*.value_type
— A string specifying the type of the value to extract. String.
indices_or_keys
type:
- String = access object member by key.
- Positive integer = access the n-th member/key from the beginning.
- Negative integer = access the n-th member/key from the end.
Returned value
Example
JSONExtractKeys
Parses a JSON string and extracts the keys.
Syntax
Parameters
json
— String with valid JSON.a, b, c...
— Comma-separated indices or keys that specify the path to the inner field in a nested JSON object. Each argument can be either a String to get the field by the key or an Integer to get the N-th field (indexed from 1, negative integers count from the end). If not set, the whole JSON is parsed as the top-level object. Optional parameter.
Returned value
Example
Query:
Result:
JSONExtractRaw
Returns part of the JSON as an unparsed string. If the part does not exist or has the wrong type, an empty string will be returned.
Syntax
Parameters
json
— JSON string to parse. String.indices_or_keys
— A list of zero or more arguments, each of which can be either string or integer. String, Int*.
indices_or_keys
type:
- String = access object member by key.
- Positive integer = access the n-th member/key from the beginning.
- Negative integer = access the n-th member/key from the end.
Returned value
- Returns part of the JSON as an unparsed string. If the part does not exist or has the wrong type, an empty string is returned. String.
Example
Case-Insensitive JSONExtract Functions
The following functions perform ASCII case-insensitive key matching when extracting values from JSON objects. They work identically to their case-sensitive counterparts, except that object keys are matched without regard to case. When multiple keys match with different cases, the first match is returned.
These functions may be less performant than their case-sensitive counterparts, so use the regular JSONExtract functions if possible.
JSONExtractIntCaseInsensitive
Parses JSON and extracts a value of Int type using case-insensitive key matching. This function is similar to JSONExtractInt
.
Syntax
Example
JSONExtractUIntCaseInsensitive
Parses JSON and extracts a value of UInt type using case-insensitive key matching. This function is similar to JSONExtractUInt
.
Syntax
Example
JSONExtractFloatCaseInsensitive
Parses JSON and extracts a value of Float type using case-insensitive key matching. This function is similar to JSONExtractFloat
.
Syntax
Example
JSONExtractBoolCaseInsensitive
Parses JSON and extracts a boolean value using case-insensitive key matching. This function is similar to JSONExtractBool
.
Syntax
Example
JSONExtractStringCaseInsensitive
Parses JSON and extracts a string using case-insensitive key matching. This function is similar to JSONExtractString
.
Syntax
Example
JSONExtractCaseInsensitive
Parses JSON and extracts a value of the given ClickHouse data type using case-insensitive key matching. This function is similar to JSONExtract
.
Syntax
Example
JSONExtractKeysAndValuesCaseInsensitive
Parses key-value pairs from JSON using case-insensitive key matching. This function is similar to JSONExtractKeysAndValues
.
Syntax
Example
JSONExtractRawCaseInsensitive
Returns part of the JSON as an unparsed string using case-insensitive key matching. This function is similar to JSONExtractRaw
.
Syntax
Example
JSONExtractArrayRawCaseInsensitive
Returns an array with elements of JSON array, each represented as unparsed string, using case-insensitive key matching. This function is similar to JSONExtractArrayRaw
.
Syntax
Example
JSONExtractKeysAndValuesRawCaseInsensitive
Extracts raw key-value pairs from JSON using case-insensitive key matching. This function is similar to JSONExtractKeysAndValuesRaw
.
Syntax
Example
JSONExtractKeysCaseInsensitive
Parses a JSON string and extracts the keys using case-insensitive key matching to navigate to nested objects. This function is similar to JSONExtractKeys
.
Syntax
Example
JSONExtractArrayRaw
Returns an array with elements of JSON array, each represented as unparsed string. If the part does not exist or isn't an array, then an empty array will be returned.
Syntax
Parameters
json
— JSON string to parse. String.indices_or_keys
— A list of zero or more arguments, each of which can be either string or integer. String, Int*.
indices_or_keys
type:
- String = access object member by key.
- Positive integer = access the n-th member/key from the beginning.
- Negative integer = access the n-th member/key from the end.
Returned value
- Returns an array with elements of JSON array, each represented as unparsed string. Otherwise, an empty array is returned if the part does not exist or is not an array. Array(String).
Example
JSONExtractKeysAndValuesRaw
Extracts raw data from a JSON object.
Syntax
Arguments
json
— String with valid JSON.p, a, t, h
— Comma-separated indices or keys that specify the path to the inner field in a nested JSON object. Each argument can be either a string to get the field by the key or an integer to get the N-th field (indexed from 1, negative integers count from the end). If not set, the whole JSON is parsed as the top-level object. Optional parameter.
Returned values
- Array with
('key', 'value')
tuples. Both tuple members are strings. Array(Tuple(String, String). - Empty array if the requested object does not exist, or input JSON is invalid. Array(Tuple(String, String).
Examples
Query:
Result:
Query:
Result:
Query:
Result:
JSON_EXISTS
If the value exists in the JSON document, 1
will be returned. If the value does not exist, 0
will be returned.
Syntax
Parameters
Before version 21.11 the order of arguments was wrong, i.e. JSON_EXISTS(path, json)
Returned value
- Returns
1
if the value exists in the JSON document, otherwise0
.
Examples
JSON_QUERY
Parses a JSON and extract a value as a JSON array or JSON object. If the value does not exist, an empty string will be returned.
Syntax
Parameters
Before version 21.11 the order of arguments was wrong, i.e. JSON_EXISTS(path, json)
Returned value
- Returns the extracted value as a JSON array or JSON object. Otherwise it returns an empty string if the value does not exist. String.
Example
Query:
Result:
JSON_VALUE
Parses a JSON and extract a value as a JSON scalar. If the value does not exist, an empty string will be returned by default.
This function is controlled by the following settings:
- by SET
function_json_value_return_type_allow_nullable
=true
,NULL
will be returned. If the value is complex type (such as: struct, array, map), an empty string will be returned by default. - by SET
function_json_value_return_type_allow_complex
=true
, the complex value will be returned.
Syntax
Parameters
Before version 21.11 the order of arguments was wrong, i.e. JSON_EXISTS(path, json)
Returned value
- Returns the extracted value as a JSON scalar if it exists, otherwise an empty string is returned. String.
Example
Query:
Result:
toJSONString
Serializes a value to its JSON representation. Various data types and nested structures are supported.
64-bit integers or bigger (like UInt64
or Int128
) are enclosed in quotes by default. output_format_json_quote_64bit_integers controls this behavior.
Special values NaN
and inf
are replaced with null
. Enable output_format_json_quote_denormals setting to show them.
When serializing an Enum value, the function outputs its name.
Syntax
Arguments
value
— Value to serialize. Value may be of any data type.
Returned value
- JSON representation of the value. String.
Example
The first example shows serialization of a Map. The second example shows some special values wrapped into a Tuple.
Query:
Result:
See Also
JSONArrayLength
Returns the number of elements in the outermost JSON array. The function returns NULL if input JSON string is invalid.
Syntax
Alias: JSON_ARRAY_LENGTH(json)
.
Arguments
json
— String with valid JSON.
Returned value
- If
json
is a valid JSON array string, returns the number of array elements, otherwise returns NULL. Nullable(UInt64).
Example
jsonMergePatch
Returns the merged JSON object string which is formed by merging multiple JSON objects.
Syntax
Arguments
json
— String with valid JSON.
Returned value
- If JSON object strings are valid, return the merged JSON object string. String.
Example
JSONAllPaths
Returns the list of all paths stored in each row in JSON column.
Syntax
Arguments
json
— JSON.
Returned value
- An array of paths. Array(String).
Example
JSONAllPathsWithTypes
Returns the map of all paths and their data types stored in each row in JSON column.
Syntax
Arguments
json
— JSON.
Returned value
- An array of paths. Map(String, String).
Example
JSONDynamicPaths
Returns the list of dynamic paths that are stored as separate subcolumns in JSON column.
Syntax
Arguments
json
— JSON.
Returned value
- An array of paths. Array(String).
Example
JSONDynamicPathsWithTypes
Returns the map of dynamic paths that are stored as separate subcolumns and their types in each row in JSON column.
Syntax
Arguments
json
— JSON.
Returned value
- An array of paths. Map(String, String).
Example
JSONSharedDataPaths
Returns the list of paths that are stored in shared data structure in JSON column.
Syntax
Arguments
json
— JSON.
Returned value
- An array of paths. Array(String).
Example
JSONSharedDataPathsWithTypes
Returns the map of paths that are stored in shared data structure and their types in each row in JSON column.
Syntax
Arguments
json
— JSON.
Returned value
- An array of paths. Map(String, String).
Example
JSONAllPaths
Introduced in: v24.8
Returns the list of all paths stored in each row in JSON column.
Syntax
Arguments
json
— JSON column.JSON
Returned value
Returns an array of all paths in the JSON column. Array(String)
Examples
Usage example
JSONAllPathsWithTypes
Introduced in: v24.8
Returns the list of all paths and their data types stored in each row in JSON column.
Syntax
Arguments
json
— JSON column.JSON
Returned value
Returns a map of all paths and their data types in the JSON column. Map(String, String)
Examples
Usage example
JSONArrayLength
Introduced in: v23.2
Returns the number of elements in the outermost JSON array.
The function returns NULL
if input JSON string is invalid.
Syntax
Arguments
json
— String with valid JSON.String
Returned value
Returns the number of array elements if json
is a valid JSON array string, otherwise returns NULL
. Nullable(UInt64)
Examples
Usage example
JSONDynamicPaths
Introduced in: v24.8
Returns the list of dynamic paths that are stored as separate subcolumns in JSON column.
Syntax
Arguments
json
— JSON column.JSON
Returned value
Returns an array of dynamic paths in the JSON column. Array(String)
Examples
Usage example
JSONDynamicPathsWithTypes
Introduced in: v24.8
Returns the list of dynamic paths that are stored as separate subcolumns and their types in each row in JSON column.
Syntax
Arguments
json
— JSON column.JSON
Returned value
Returns a map of dynamic paths and their data types in the JSON column. Map(String, String)
Examples
Usage example
JSONExtract
Introduced in: v19.14
Parses JSON and extracts a value with given ClickHouse data type.
Syntax
Arguments
json
— JSON string to parse.String
return_type
— ClickHouse data type to return.String
indices_or_keys
— A list of zero or more arguments each of which can be either string or integer.String
or(U)Int*
Returned value
Returns a value of specified ClickHouse data type if possible, otherwise returns the default value for that type.
Examples
Usage example
JSONExtractArrayRaw
Introduced in: v20.1
Returns an array with elements of JSON array, each represented as unparsed string.
Syntax
Arguments
json
— JSON string to parse.String
indices_or_keys
— A list of zero or more arguments each of which can be either string or integer.String
or(U)Int*
Returned value
Returns an array of strings with JSON array elements. If the part is not an array or does not exist, an empty array will be returned. Array(String)
Examples
Usage example
JSONExtractArrayRawCaseInsensitive
Introduced in: v25.8
Returns an array with elements of JSON array, each represented as unparsed string, using case-insensitive key matching. This function is similar to JSONExtractArrayRaw
.
Syntax
Arguments
json
— JSON string to parseString
indices_or_keys
— Optional. Indices or keys to navigate to the array. Keys use case-insensitive matchingString
or(U)Int*
Returned value
Returns an array of raw JSON strings. Array(String)
Examples
basic
JSONExtractBool
Introduced in: v20.1
Parses JSON and extracts a value of Bool type.
Syntax
Arguments
json
— JSON string to parse.String
indices_or_keys
— A list of zero or more arguments each of which can be either string or integer.String
or(U)Int*
Returned value
Returns a Bool value if it exists, otherwise returns 0
. Bool
Examples
Usage example
JSONExtractBoolCaseInsensitive
Introduced in: v25.8
Parses JSON and extracts a boolean value using case-insensitive key matching. This function is similar to JSONExtractBool
.
Syntax
Arguments
json
— JSON string to parseString
indices_or_keys
— Optional. Indices or keys to navigate to the field. Keys use case-insensitive matchingString
or(U)Int*
Returned value
Returns the extracted boolean value (1 for true, 0 for false), 0 if not found. UInt8
Examples
basic
JSONExtractCaseInsensitive
Introduced in: v25.8
Parses JSON and extracts a value of the given ClickHouse data type using case-insensitive key matching. This function is similar to JSONExtract
.
Syntax
Arguments
json
— JSON string to parseString
indices_or_keys
— Optional. Indices or keys to navigate to the field. Keys use case-insensitive matchingString
or(U)Int*
return_type
— The ClickHouse data type to extractString
Returned value
Returns the extracted value in the specified data type. Any
Examples
int_type
array_type
JSONExtractFloat
Introduced in: v20.1
Parses JSON and extracts a value of Float type.
Syntax
Arguments
json
— JSON string to parse.String
indices_or_keys
— A list of zero or more arguments each of which can be either string or integer.String
or(U)Int*
Returned value
Returns a Float value if it exists, otherwise returns 0
. Float64
Examples
Usage example
JSONExtractFloatCaseInsensitive
Introduced in: v25.8
Parses JSON and extracts a value of Float type using case-insensitive key matching. This function is similar to JSONExtractFloat
.
Syntax
Arguments
json
— JSON string to parseString
indices_or_keys
— Optional. Indices or keys to navigate to the field. Keys use case-insensitive matchingString
or(U)Int*
Returned value
Returns the extracted Float value, 0 if not found or cannot be converted. Float64
Examples
basic
JSONExtractInt
Introduced in: v20.1
Parses JSON and extracts a value of Int type.
Syntax
Arguments
json
— JSON string to parse.String
indices_or_keys
— A list of zero or more arguments each of which can be either string or integer.String
or(U)Int*
Returned value
Returns an Int value if it exists, otherwise returns 0
. Int64
Examples
Usage example
JSONExtractIntCaseInsensitive
Introduced in: v25.8
Parses JSON and extracts a value of Int type using case-insensitive key matching. This function is similar to JSONExtractInt
.
Syntax
Arguments
json
— JSON string to parseString
indices_or_keys
— Optional. Indices or keys to navigate to the field. Keys use case-insensitive matchingString
or(U)Int*
Returned value
Returns the extracted Int value, 0 if not found or cannot be converted. Int64
Examples
basic
nested
JSONExtractKeys
Introduced in: v21.11
Parses a JSON string and extracts the keys.
Syntax
Arguments
json
— JSON string to parse.String
indices_or_keys
— A list of zero or more arguments each of which can be either string or integer.String
or(U)Int*
Returned value
Returns an array with the keys of the JSON object. Array(String)
Examples
Usage example
JSONExtractKeysAndValues
Introduced in: v20.1
Parses key-value pairs from a JSON where the values are of the given ClickHouse data type.
Syntax
Arguments
json
— JSON string to parse.String
value_type
— ClickHouse data type of the values.String
indices_or_keys
— A list of zero or more arguments each of which can be either string or integer.String
or(U)Int*
Returned value
Returns an array of tuples with the parsed key-value pairs. Array(Tuple(String, value_type))
Examples
Usage example
JSONExtractKeysAndValuesCaseInsensitive
Introduced in: v25.8
Parses key-value pairs from JSON using case-insensitive key matching. This function is similar to JSONExtractKeysAndValues
.
Syntax
Arguments
json
— JSON string to parseString
indices_or_keys
— Optional. Indices or keys to navigate to the object. Keys use case-insensitive matchingString
or(U)Int*
value_type
— The ClickHouse data type of the valuesString
Returned value
Returns an array of tuples containing key-value pairs. Array(Tuple(String, T))
Examples
basic
JSONExtractKeysAndValuesRaw
Introduced in: v20.4
Returns an array of tuples with keys and values from a JSON object. All values are represented as unparsed strings.
Syntax
Arguments
json
— JSON string to parse.String
indices_or_keys
— A list of zero or more arguments each of which can be either string or integer.String
or(U)Int*
Returned value
Returns an array of tuples with parsed key-value pairs where values are unparsed strings. Array(Tuple(String, String))
Examples
Usage example
JSONExtractKeysAndValuesRawCaseInsensitive
Introduced in: v25.8
Extracts raw key-value pairs from JSON using case-insensitive key matching. This function is similar to JSONExtractKeysAndValuesRaw
.
Syntax
Arguments
json
— JSON string to parseString
indices_or_keys
— Optional. Indices or keys to navigate to the object. Keys use case-insensitive matchingString
or(U)Int*
Returned value
Returns an array of tuples containing key-value pairs as raw strings. Array(Tuple(String, String))
Examples
basic
JSONExtractKeysCaseInsensitive
Introduced in: v25.8
Parses a JSON string and extracts the keys using case-insensitive key matching to navigate to nested objects. This function is similar to JSONExtractKeys
.
Syntax
Arguments
json
— JSON string to parseString
indices_or_keys
— Optional. Indices or keys to navigate to the object. Keys use case-insensitive matchingString
or(U)Int*
Returned value
Returns an array of keys from the JSON object. Array(String)
Examples
basic
nested
JSONExtractRaw
Introduced in: v20.1
Returns a part of JSON as unparsed string.
Syntax
Arguments
json
— JSON string to parse.String
indices_or_keys
— A list of zero or more arguments each of which can be either string or integer.String
or(U)Int*
Returned value
Returns the part of JSON as an unparsed string. If the part does not exist or has a wrong type, an empty string will be returned. String
Examples
Usage example
JSONExtractRawCaseInsensitive
Introduced in: v25.8
Returns part of the JSON as an unparsed string using case-insensitive key matching. This function is similar to JSONExtractRaw
.
Syntax
Arguments
json
— JSON string to parseString
indices_or_keys
— Optional. Indices or keys to navigate to the field. Keys use case-insensitive matchingString
or(U)Int*
Returned value
Returns the raw JSON string of the extracted element. String
Examples
object
JSONExtractString
Introduced in: v20.1
Parses JSON and extracts a value of String type.
Syntax
Arguments
json
— JSON string to parse.String
indices_or_keys
— A list of zero or more arguments each of which can be either string or integer.String
or(U)Int*
Returned value
Returns a String value if it exists, otherwise returns an empty string. String
Examples
Usage example
JSONExtractStringCaseInsensitive
Introduced in: v25.8
Parses JSON and extracts a string using case-insensitive key matching. This function is similar to JSONExtractString
.
Syntax
Arguments
json
— JSON string to parseString
indices_or_keys
— Optional. Indices or keys to navigate to the field. Keys use case-insensitive matchingString
or(U)Int*
Returned value
Returns the extracted string value, empty string if not found. String
Examples
basic
nested
JSONExtractUInt
Introduced in: v20.1
Parses JSON and extracts a value of UInt type.
Syntax
Arguments
json
— JSON string to parse.String
indices_or_keys
— A list of zero or more arguments each of which can be either string or integer.String
or(U)Int*
Returned value
Returns a UInt value if it exists, otherwise returns 0
. UInt64
Examples
Usage example
JSONExtractUIntCaseInsensitive
Introduced in: v25.8
Parses JSON and extracts a value of UInt type using case-insensitive key matching. This function is similar to JSONExtractUInt
.
Syntax
Arguments
json
— JSON string to parseString
indices_or_keys
— Optional. Indices or keys to navigate to the field. Keys use case-insensitive matchingString
or(U)Int*
Returned value
Returns the extracted UInt value, 0 if not found or cannot be converted. UInt64
Examples
basic
JSONHas
Introduced in: v20.1
Checks for the existence of the provided value(s) in the JSON document.
Syntax
Arguments
json
— JSON string to parseString
[ ,indices_or_keys, ...]
— A list of zero or more arguments.String
or(U)Int*
Returned value
Returns 1
if the value exists in json
, otherwise 0
UInt8
Examples
Usage example
JSONLength
Introduced in: v20.1
Return the length of a JSON array or a JSON object.
If the value does not exist or has the wrong type, 0
will be returned.
Syntax
Arguments
json
— JSON string to parseString
[, indices_or_keys, ...]
— Optional. A list of zero or more arguments.String
or(U)Int8/16/32/64
Returned value
Returns the length of the JSON array or JSON object, otherwise returns 0
if the value does not exist or has the wrong type. UInt64
Examples
Usage example
JSONMergePatch
Introduced in: v23.10
Returns the merged JSON object string which is formed by merging multiple JSON objects.
Syntax
Arguments
json1[, json2, ...]
— One or more strings with valid JSON.String
Returned value
Returns the merged JSON object string, if the JSON object strings are valid. String
Examples
Usage example
JSONSharedDataPaths
Introduced in: v24.8
Returns the list of paths that are stored in shared data structure in JSON column.
Syntax
Arguments
json
— JSON column.JSON
Returned value
Returns an array of paths stored in shared data structure in the JSON column. Array(String)
Examples
Usage example
JSONSharedDataPathsWithTypes
Introduced in: v24.8
Returns the list of paths that are stored in shared data structure and their types in each row in JSON column.
Syntax
Arguments
json
— JSON column.JSON
Returned value
Returns a map of paths stored in shared data structure and their data types in the JSON column. Map(String, String)
Examples
Usage example
JSONType
Introduced in: v20.1
Return the type of a JSON value. If the value does not exist, Null=0
will be returned.
Syntax
Arguments
json
— JSON string to parseString
json[, indices_or_keys, ...]
— A list of zero or more arguments, each of which can be either string or integer.String
or(U)Int8/16/32/64
Returned value
Returns the type of a JSON value as a string, otherwise if the value doesn't exist it returns Null=0
Enum
Examples
Usage example
JSON_EXISTS
Introduced in: v21.8
If the value exists in the JSON document, 1
will be returned.
If the value does not exist, 0
will be returned.
Syntax
Arguments
Returned value
Returns 1
if the value exists in the JSON document, otherwise 0
. UInt8
Examples
Usage example
JSON_QUERY
Introduced in: v21.8
Parses a JSON and extract a value as a JSON array or JSON object. If the value does not exist, an empty string will be returned.
Syntax
Arguments
Returned value
Returns the extracted JSON array or JSON object as a string, or an empty string if the value does not exist. String
Examples
Usage example
JSON_VALUE
Introduced in: v21.11
Parses a JSON and extract a value as a JSON scalar. If the value does not exist, an empty string will be returned by default.
This function is controlled by the following settings:
- by SET
function_json_value_return_type_allow_nullable
=true
,NULL
will be returned. If the value is complex type (such as: struct, array, map), an empty string will be returned by default. - by SET
function_json_value_return_type_allow_complex
=true
, the complex value will be returned.
Syntax
Arguments
Returned value
Returns the extracted JSON scalar as a string, or an empty string if the value does not exist. String
Examples
Usage example
dynamicElement
Introduced in: v
Extracts a column with specified type from a Dynamic
column.
Syntax
Arguments
dynamic
— Dynamic column -type_name
— The name of the variant type to extract
Returned value
Examples
Example
dynamicType
Introduced in: v
Returns the variant type name for each row of Dynamic
column. If row contains NULL, it returns 'None' for it.
Syntax
Arguments
dynamic
— Dynamic column
Returned value
Examples
Example
isDynamicElementInSharedData
Introduced in: v
Returns true for rows in Dynamic column that are not separated into subcolumns and stored inside shared variant in binary form.
Syntax
Arguments
dynamic
— Dynamic column
Returned value
Examples
Example
isValidJSON
Introduced in: v20.1
Checks that the string passed is valid JSON.
Syntax
Arguments
json
— JSON string to validateString
Returned value
Returns 1
if the string is valid JSON, otherwise 0
. UInt8
Examples
Usage example
Using integers to access both JSON arrays and JSON objects
simpleJSONExtractBool
Introduced in: v21.4
Parses a true/false value from the value of the field named field_name
.
The result is UInt8
.
Syntax
Arguments
json
— The JSON in which the field is searched for.String
field_name
— The name of the field to search for.const String
Returned value
Returns 1
if the value of the field is true
, 0
otherwise. This means this function will return 0
including (and not only) in the following cases:
- If the field doesn't exists.
- If the field contains
true
as a string, e.g.:{"field":"true"}
. - If the field contains
1
as a numerical value.UInt8
Examples
Usage example
simpleJSONExtractFloat
Introduced in: v21.4
Parses Float64
from the value of the field named field_name
.
If field_name
is a string field, it tries to parse a number from the beginning of the string.
If the field does not exist, or it exists but does not contain a number, it returns 0
.
Syntax
Arguments
json
— The JSON in which the field is searched for.String
field_name
— The name of the field to search for.const String
Returned value
Returns the number parsed from the field if the field exists and contains a number, otherwise 0
. Float64
Examples
Usage example
simpleJSONExtractInt
Introduced in: v21.4
Parses Int64
from the value of the field named field_name
.
If field_name
is a string field, it tries to parse a number from the beginning of the string.
If the field does not exist, or it exists but does not contain a number, it returns 0
.
Syntax
Arguments
json
— The JSON in which the field is searched for.String
field_name
— The name of the field to search for.const String
Returned value
Returns the number parsed from the field if the field exists and contains a number, 0
otherwise Int64
Examples
Usage example
simpleJSONExtractRaw
Introduced in: v21.4
Returns the value of the field named field_name
as a String
, including separators.
Syntax
Arguments
json
— The JSON in which the field is searched for.String
field_name
— The name of the field to search for.const String
Returned value
Returns the value of the field as a string, including separators if the field exists, or an empty string otherwise String
Examples
Usage example
simpleJSONExtractString
Introduced in: v21.4
Parses String
in double quotes from the value of the field named field_name
.
Implementation details
There is currently no support for code points in the format \uXXXX\uYYYY
that are not from the basic multilingual plane (they are converted to CESU-8 instead of UTF-8).
Syntax
Arguments
json
— The JSON in which the field is searched for.String
field_name
— The name of the field to search for.const String
Returned value
Returns the unescaped value of a field as a string, including separators. An empty string is returned if the field doesn't contain a double quoted string, if unescaping fails or if the field doesn't exist String
Examples
Usage example
simpleJSONExtractUInt
Introduced in: v21.4
Parses UInt64
from the value of the field named field_name
.
If field_name
is a string field, it tries to parse a number from the beginning of the string.
If the field does not exist, or it exists but does not contain a number, it returns 0
.
Syntax
Arguments
json
— The JSON in which the field is searched for.String
field_name
— The name of the field to search for.const String
Returned value
Returns the number parsed from the field if the field exists and contains a number, 0
otherwise UInt64
Examples
Usage example
simpleJSONHas
Introduced in: v21.4
Checks whether there is a field named field_name
.
Syntax
Arguments
json
— The JSON in which the field is searched for.String
field_name
— The name of the field to search for.const String
Returned value
Returns 1
if the field exists, 0
otherwise UInt8
Examples
Usage example
toJSONString
Introduced in: v21.7
Serializes a value to its JSON representation. Various data types and nested structures are supported.
64-bit integers or bigger (like UInt64
or Int128
) are enclosed in quotes by default. output_format_json_quote_64bit_integers controls this behavior.
Special values NaN
and inf
are replaced with null
. Enable output_format_json_quote_denormals setting to show them.
When serializing an Enum value, the function outputs its name.
See also:
Syntax
Arguments
value
— Value to serialize. Value may be of any data type.Any
Returned value
Returns the JSON representation of the value. String
Examples
Map serialization
Special values
variantElement
Introduced in: v
Extracts a column with specified type from a Variant
column.
Syntax
Arguments
variant
— Variant column -type_name
— The name of the variant type to extract -default_value
— The default value that will be used if variant doesn't have variant with specified type. Can be any type. Optional
Returned value
Examples
Example
variantType
Introduced in: v
Returns the variant type name for each row of Variant
column. If row contains NULL, it returns 'None' for it.
Syntax
Arguments
variant
— Variant column
Returned value
Examples
Example