We make the following assumptions about Json format:
visitParamHas('{"abc":"def"}', 'abc') = 1, but visitParamHas('{"\u0061\u0062\u0063":"def"}', 'abc') = 0Checks whether a field with the name «name» exists.
Parses the value of the field named «name» as UInt64. If this is a string field, the function will try to parse a number from the beginning of the string. If the field does not exist, or no number can be parsed from it, 0 is returned.
Same as visitParamExtractUInt, but returns Int64.
Same as visitParamExtractUInt, but returns Float64.
Parses true/false values. The result is of UInt8 type.
Returns the value of the field, including whitespace characters.
Example:
visitParamExtractRaw('{"abc":"\n\u0000"}', 'abc') = '"\n\u0000"' visitParamExtractRaw('{"abc":{"def":[1,2,3]}}', 'abc') = '{"def":[1,2,3]}'
Parses the string using double quotes. This value is not escaped. If escaping fails, it will return an empty string.
Example:
visitParamExtractString('{"abc":"\n\u0000"}', 'abc') = '\n\0' visitParamExtractString('{"abc":"\u263a"}', 'abc') = '☺' visitParamExtractString('{"abc":"\u263"}', 'abc') = '' visitParamExtractString('{"abc":"hello}', 'abc') = ''
Currently, \uXXXX\uYYYY character encodings are not supported. These encodings are not in the basic multilingual plane (they are converted to CESU-8 instead of UTF-8).
The following functions are based on simdjson and are designed for more complex JSON parsing requirements. However, assumption 2 above still applies.
If the value exists in JSON, 1 is returned.
If the value does not exist, 0 is returned.
Example:
select JSONHas('{"a": "hello", "b": [-100, 200.0, 300]}', 'b') = 1 select JSONHas('{"a": "hello", "b": [-100, 200.0, 300]}', 'b', 4) = 0
indices_or_keys can be a list of zero or more parameters, each parameter can be a string or integer.
You can use integers to access JSON arrays and JSON objects.
For example:
select JSONExtractKey('{"a": "hello", "b": [-100, 200.0, 300]}', 1) = 'a' select JSONExtractKey('{"a": "hello", "b": [-100, 200.0, 300]}', 2) = 'b' select JSONExtractKey('{"a": "hello", "b": [-100, 200.0, 300]}', -1) = 'b' select JSONExtractKey('{"a": "hello", "b": [-100, 200.0, 300]}', -2) = 'a' select JSONExtractString('{"a": "hello", "b": [-100, 200.0, 300]}', 1) = 'hello'
Returns the length of a JSON array or JSON object.
If the value does not exist or the type is incorrect, 0 is returned.
Example:
select JSONLength('{"a": "hello", "b": [-100, 200.0, 300]}', 'b') = 3 select JSONLength('{"a": "hello", "b": [-100, 200.0, 300]}') = 2
If the value does not exist, Null is returned.
Example:
select JSONType('{"a": "hello", "b": [-100, 200.0, 300]}') = 'Object' select JSONType('{"a": "hello", "b": [-100, 200.0, 300]}', 'a') = 'String' select JSONType('{"a": "hello", "b": [-100, 200.0, 300]}', 'b') = 'Array'
Parses JSON and extracts values. These functions are similar to the visitParam* functions.
If the value does not exist or the type is incorrect, it returns 0.
Example:
select JSONExtractInt('{"a": "hello", "b": [-100, 200.0, 300]}', 'b', 1) = -100 select JSONExtractFloat('{"a": "hello", "b": [-100, 200.0, 300]}', 'b', 2) = 200.0 select JSONExtractUInt('{"a": "hello", "b": [-100, 200.0, 300]}', 'b', -1) = 300
Parse JSON and extract a string. This function is similar to the visitParamExtractString function.
If the value does not exist or the type is incorrect, it returns an empty string.
The value is not escaped. If unescaping fails, it returns an empty string.
Example:
select JSONExtractString('{"a": "hello", "b": [-100, 200.0, 300]}', 'a') = 'hello' select JSONExtractString('{"abc":"\n\u0000"}', 'abc') = '\n\0' select JSONExtractString('{"abc":"\u263a"}', 'abc') = '☺' select JSONExtractString('{"abc":"\u263"}', 'abc') = '' select JSONExtractString('{"abc":"hello}', 'abc') = ''
Parse JSON and extract the value of the given ClickHouse data type.
This is a variant of the previous JSONExtract<type> function. This means JSONExtract(…, 'String') returns exactly the same result as JSONExtractString() returns. JSONExtract(…, 'Float64') returns exactly the same result as JSONExtractFloat() returns.
Example:
SELECT JSONExtract('{"a": "hello", "b": [-100, 200.0, 300]}', 'Tuple(String, Array(Float64))') = ('hello',[-100,200,300]) SELECT JSONExtract('{"a": "hello", "b": [-100, 200.0, 300]}', 'Tuple(b Array(Float64), a String)') = ([-100,200,300],'hello') SELECT JSONExtract('{"a": "hello", "b": [-100, 200.0, 300]}', 'b', 'Array(Nullable(Int8))') = [-100, NULL, NULL] SELECT JSONExtract('{"a": "hello", "b": [-100, 200.0, 300]}', 'b', 4, 'Nullable(Int64)') = NULL SELECT JSONExtract('{"passed": true}', 'passed', 'UInt8') = 1 SELECT JSONExtract('{"day": "Thursday"}', 'day', 'Enum8('Sunday' = 0, 'Monday' = 1, 'Tuesday' = 2, 'Wednesday' = 3, 'Thursday' = 4, 'Friday' = 5, 'Saturday' = 6)') = 'Thursday' SELECT JSONExtract('{"day": 5}', 'day', 'Enum8('Sunday' = 0, 'Monday' = 1, 'Tuesday' = 2, 'Wednesday' = 3, 'Thursday' = 4, 'Friday' = 5, 'Saturday' = 6)') = 'Friday'
Parse key-value pairs from JSON, where the value is of the given ClickHouse data type.
Example:
SELECT JSONExtractKeysAndValues('{"x": {"a": 5, "b": 7, "c": 11}}', 'x', 'Int8') = [('a',5),('b',7),('c',11)];
Returns a part of the JSON.
If the part does not exist or the type is incorrect, it returns an empty string.
Example:
select JSONExtractRaw('{"a": "hello", "b": [-100, 200.0, 300]}', 'b') = '[-100, 200.0, 300]'
Checks whether the JSON path exists. 1 means it exists, 0 means it does not exist.
Syntax
JSON_EXISTS(json, path)
Param
json — valid JSON. String/Object.path — JSON subelement path. StringReturn value
Example
SELECT JSON_EXISTS('{"hello":1}', '$.hello'); SELECT JSON_EXISTS('{"hello":{"world":1}}', '$.hello.world'); SELECT JSON_EXISTS('{"hello":["world"]}', '$.hello[*]'); SELECT JSON_EXISTS('{"hello":["world"]}', '$.hello[0]');
Parses the JSON object or JSON array pointed to by the path. If the path does not exist, it returns an empty string.
Syntax
JSON_QUERY(json, path)
Param
json — a valid JSON. String/Objectpath — JSON path in string form. String.Return value
Example
Query:
SELECT JSON_QUERY('{"hello":"world"}', '$.hello'); SELECT JSON_QUERY('{"array":[[0, 1, 2, 3, 4, 5], [0, -1, -2, -3, -4, -5]]}', '$.array[*][0 to 2, 4]'); SELECT JSON_QUERY('{"hello":2}', '$.hello'); SELECT toTypeName(JSON_QUERY('{"hello":2}', '$.hello'));
Result:
["world"] [0, 1, 4, 0, -1, -4] [2] String
Parses the scalar value of the JSON object pointed to by the JSON path. If it does not exist, it returns an empty string.
function_json_value_return_type_allow_nullable = true, if the JSON path points to a complex type (such as struct, array, or map), it returns NULL.function_json_value_return_type_allow_complex = true, if the JSON path points to a complex type, it returns the value in string form.Syntax
JSON_VALUE(json, path)
Param
json — A string with valid JSON. String/Objectpath — A string representing the path. StringReturn value
Example
Query:
SELECT JSON_VALUE('{"hello":"world"}', '$.hello'); SELECT JSON_VALUE('{"array":[[0, 1, 2, 3, 4, 5], [0, -1, -2, -3, -4, -5]]}', '$.array[*][0 to 2, 4]'); SELECT JSON_VALUE('{"hello":2}', '$.hello'); SELECT toTypeName(JSON_VALUE('{"hello":2}', '$.hello')); select JSON_VALUE('{"hello":"world"}', '$.b') settings function_json_value_return_type_allow_nullable=true; select JSON_VALUE('{"hello":{"world":"!"}}', '$.hello') settings function_json_value_return_type_allow_complex=true;
Result:
world 0 2 String
Serializes the parameter type to a JSON-formatted string.
Syntax
toJSONString(value)
Param
value — Value to serialize. Value may be of any data type.Return value
Example
Query:
SELECT toJSONString(map('key1', 1, 'key2', 2)); SELECT toJSONString(tuple(1.25, NULL, NaN, +inf, -inf, [])) SETTINGS output_format_json_quote_denormals = 1;
Result:
{"key1":1,"key2":2} [1.25,null,"nan","inf","-inf",[]]
Returns the number of outermost elements in the JSON array.
Syntax
JSONArrayLength(json)
Alias: JSON_ARRAY_LENGTH(json).
Param
json — valid JSON. String/ObjectReturn value
Example
SELECT JSONArrayLength(''), JSONArrayLength('[1,2,3]') ┌─JSONArrayLength('')─┬─JSONArrayLength('[1,2,3]')─┐ │ ᴺᵁᴸᴸ │ 3 │ └─────────────────────┴────────────────────────────┘
Returns
Syntax
get_json_object(json, path)
Param
'{"test_key":"\ntest_value"}'Return value
Example
select get_json_object('{"key":"value"}', '$.key'); --value select get_json_object(null, '$.key'); --NULL select get_json_object('{"key":"\nvalue"}', '$.key'); -- select get_json_object('{"key":"\nvalue"}', '$.key'); --\nvalue