2018-03-25 02:04:22 +00:00
# Functions for working with JSON
2017-12-28 15:13:23 +00:00
In Yandex.Metrica, JSON is transmitted by users as session parameters. There are some special functions for working with this JSON. (Although in most of the cases, the JSONs are additionally pre-processed, and the resulting values are put in separate columns in their processed format.) All these functions are based on strong assumptions about what the JSON can be, but they try to do as little as possible to get the job done.
The following assumptions are made:
1. The field name (function argument) must be a constant.
2018-03-26 13:16:59 +00:00
2. The field name is somehow canonically encoded in JSON. For example: `visitParamHas('{"abc":"def"}', 'abc') = 1` , but `visitParamHas('{"\\u0061\\u0062\\u0063":"def"}', 'abc') = 0`
2017-12-28 15:13:23 +00:00
3. Fields are searched for on any nesting level, indiscriminately. If there are multiple matching fields, the first occurrence is used.
4. The JSON doesn't have space characters outside of string literals.
## visitParamHas(params, name)
Checks whether there is a field with the 'name' name.
## visitParamExtractUInt(params, name)
Parses UInt64 from the value of the field named 'name'. If this is a string field, it tries to parse a number from the beginning of the string. If the field doesn't exist, or it exists but doesn't contain a number, it returns 0.
## visitParamExtractInt(params, name)
The same as for Int64.
## visitParamExtractFloat(params, name)
The same as for Float64.
## visitParamExtractBool(params, name)
Parses a true/false value. The result is UInt8.
## visitParamExtractRaw(params, name)
Returns the value of a field, including separators.
Examples:
2019-09-23 15:31:46 +00:00
```sql
2017-12-28 15:13:23 +00:00
visitParamExtractRaw('{"abc":"\\n\\u0000"}', 'abc') = '"\\n\\u0000"'
visitParamExtractRaw('{"abc":{"def":[1,2,3]}}', 'abc') = '{"def":[1,2,3]}'
```
## visitParamExtractString(params, name)
Parses the string in double quotes. The value is unescaped. If unescaping failed, it returns an empty string.
Examples:
2019-09-23 15:31:46 +00:00
```sql
2018-03-25 02:04:22 +00:00
visitParamExtractString('{"abc":"\\n\\u0000"}', 'abc') = '\n\0'
visitParamExtractString('{"abc":"\\u263a"}', 'abc') = '☺'
visitParamExtractString('{"abc":"\\u263"}', 'abc') = ''
visitParamExtractString('{"abc":"hello}', 'abc') = ''
2017-12-28 15:13:23 +00:00
```
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).
2019-04-24 04:23:14 +00:00
The following functions are based on [simdjson ](https://github.com/lemire/simdjson ) designed for more complex JSON parsing requirements. The assumption 2 mentioned above still applies.
2019-10-12 12:00:46 +00:00
## isValidJSON(json)
Checks that passed string is a valid json.
Examples:
```sql
SELECT isValidJSON('{"a": "hello", "b": [-100, 200.0, 300]}') = 1
SELECT isValidJSON('not a json') = 0
```
2019-05-16 13:35:31 +00:00
## JSONHas(json[, indices_or_keys]...)
2019-04-24 04:23:14 +00:00
If the value exists in the JSON document, `1` will be returned.
2019-05-16 13:35:31 +00:00
If the value does not exist, `0` will be returned.
2019-04-24 04:23:14 +00:00
Examples:
2019-09-23 15:31:46 +00:00
```sql
SELECT JSONHas('{"a": "hello", "b": [-100, 200.0, 300]}', 'b') = 1
SELECT JSONHas('{"a": "hello", "b": [-100, 200.0, 300]}', 'b', 4) = 0
2019-04-24 04:23:14 +00:00
```
2019-05-16 13:35:31 +00:00
`indices_or_keys` is a list of zero or more arguments each of them can be either string or integer.
2019-04-24 04:23:14 +00:00
* 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.
2019-06-14 12:44:33 +00:00
Minimum index of the element is 1. Thus the element 0 doesn't exist.
2019-05-10 08:49:03 +00:00
You may use integers to access both JSON arrays and JSON objects.
2019-04-24 04:23:14 +00:00
So, for example:
2019-09-23 15:31:46 +00:00
```sql
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'
2019-04-24 04:23:14 +00:00
```
2019-05-16 13:35:31 +00:00
## JSONLength(json[, indices_or_keys]...)
2019-04-24 04:23:14 +00:00
2019-05-10 08:49:03 +00:00
Return the length of a JSON array or a JSON object.
2019-04-24 04:23:14 +00:00
2019-05-16 13:35:31 +00:00
If the value does not exist or has a wrong type, `0` will be returned.
2019-04-24 04:23:14 +00:00
Examples:
2019-09-23 15:31:46 +00:00
```sql
SELECT JSONLength('{"a": "hello", "b": [-100, 200.0, 300]}', 'b') = 3
SELECT JSONLength('{"a": "hello", "b": [-100, 200.0, 300]}') = 2
2019-04-24 04:23:14 +00:00
```
2019-05-16 13:35:31 +00:00
## JSONType(json[, indices_or_keys]...)
2019-04-24 04:23:14 +00:00
Return the type of a JSON value.
2019-05-16 13:35:31 +00:00
If the value does not exist, `Null` will be returned.
2019-04-24 04:23:14 +00:00
Examples:
2019-09-23 15:31:46 +00:00
```sql
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'
2019-04-24 04:23:14 +00:00
```
2019-05-16 13:35:31 +00:00
## JSONExtractUInt(json[, indices_or_keys]...)
## JSONExtractInt(json[, indices_or_keys]...)
## JSONExtractFloat(json[, indices_or_keys]...)
## JSONExtractBool(json[, indices_or_keys]...)
2019-04-24 04:23:14 +00:00
2019-05-16 13:35:31 +00:00
Parses a JSON and extract a value. These functions are similar to `visitParam` functions.
2019-04-24 04:23:14 +00:00
2019-05-16 13:35:31 +00:00
If the value does not exist or has a wrong type, `0` will be returned.
2019-04-24 04:23:14 +00:00
Examples:
2019-09-23 15:31:46 +00:00
```sql
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
2019-04-24 04:23:14 +00:00
```
2019-05-16 13:35:31 +00:00
## JSONExtractString(json[, indices_or_keys]...)
2019-04-24 04:23:14 +00:00
2019-05-16 13:35:31 +00:00
Parses a JSON and extract a string. This function is similar to `visitParamExtractString` functions.
2019-04-24 04:23:14 +00:00
2019-05-16 13:35:31 +00:00
If the value does not exist or has a wrong type, an empty string will be returned.
2019-04-24 04:23:14 +00:00
2019-05-16 13:35:31 +00:00
The value is unescaped. If unescaping failed, it returns an empty string.
2019-04-24 04:23:14 +00:00
Examples:
2019-09-23 15:31:46 +00:00
```sql
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') = ''
2019-04-24 04:23:14 +00:00
```
2019-05-16 13:35:31 +00:00
## JSONExtract(json[, indices_or_keys...], return_type)
2019-05-13 13:46:19 +00:00
2019-05-16 13:35:31 +00:00
Parses a JSON and extract a value of the given ClickHouse data type.
2019-05-13 13:46:19 +00:00
2019-05-16 13:35:31 +00:00
This is a generalization of the previous `JSONExtract<type>` functions.
This means
`JSONExtract(..., 'String')` returns exactly the same as `JSONExtractString()` ,
`JSONExtract(..., 'Float64')` returns exactly the same as `JSONExtractFloat()` .
2019-05-13 13:46:19 +00:00
Examples:
2019-09-23 15:31:46 +00:00
```sql
2019-05-16 13:35:31 +00:00
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'
```
## JSONExtractKeysAndValues(json[, indices_or_keys...], value_type)
Parse key-value pairs from a JSON where the values are of the given ClickHouse data type.
Example:
2019-09-23 15:31:46 +00:00
```sql
2019-05-16 13:35:31 +00:00
SELECT JSONExtractKeysAndValues('{"x": {"a": 5, "b": 7, "c": 11}}', 'x', 'Int8') = [('a',5),('b',7),('c',11)];
2019-05-13 13:46:19 +00:00
```
2019-05-16 13:35:31 +00:00
## JSONExtractRaw(json[, indices_or_keys]...)
Returns a part of JSON.
If the part does not exist or has a wrong type, an empty string will be returned.
Example:
2019-09-23 15:31:46 +00:00
```sql
SELECT JSONExtractRaw('{"a": "hello", "b": [-100, 200.0, 300]}', 'b') = '[-100, 200.0, 300]'
2019-05-16 13:35:31 +00:00
```
2019-06-05 10:04:00 +00:00
[Original article ](https://clickhouse.yandex/docs/en/query_language/functions/json_functions/ ) <!--hide-->