2020-04-03 13:23:32 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/functions/json-functions
2023-04-19 17:05:55 +00:00
sidebar_position: 105
2022-04-09 13:29:05 +00:00
sidebar_label: JSON
2020-04-03 13:23:32 +00:00
---
2024-05-28 11:43:22 +00:00
There are two sets of functions to parse JSON:
2024-06-12 12:09:37 +00:00
- [`simpleJSON*` (`visitParam*`) ](#simplejson-visitparam-functions ) which is made for parsing a limited subset of JSON extremely fast.
2024-05-28 12:48:10 +00:00
- [`JSONExtract*` ](#jsonextract-functions ) which is made for parsing ordinary JSON.
2023-02-14 14:35:25 +00:00
2024-06-12 12:09:37 +00:00
## simpleJSON (visitParam) functions
2023-02-14 14:35:25 +00:00
2024-05-28 11:43:22 +00:00
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.
2022-04-09 13:29:05 +00:00
The following assumptions are made:
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
1. The field name (function argument) must be a constant.
2024-03-05 16:55:08 +00:00
2. The field name is somehow canonically encoded in JSON. For example: `simpleJSONHas('{"abc":"def"}', 'abc') = 1` , but `simpleJSONHas('{"\\u0061\\u0062\\u0063":"def"}', 'abc') = 0`
2020-03-20 10:10:48 +00:00
3. Fields are searched for on any nesting level, indiscriminately. If there are multiple matching fields, the first occurrence is used.
2021-05-27 19:44:11 +00:00
4. The JSON does not have space characters outside of string literals.
2017-12-28 15:13:23 +00:00
2024-05-28 12:48:10 +00:00
### simpleJSONHas
2017-12-28 15:13:23 +00:00
2024-03-05 16:55:08 +00:00
Checks whether there is a field named `field_name` . The result is `UInt8` .
2021-04-17 17:04:32 +00:00
2024-03-05 16:55:08 +00:00
**Syntax**
2017-12-28 15:13:23 +00:00
2024-03-05 16:55:08 +00:00
```sql
simpleJSONHas(json, field_name)
```
2024-05-28 11:43:22 +00:00
Alias: `visitParamHas` .
2024-03-05 16:55:08 +00:00
**Parameters**
2017-12-28 15:13:23 +00:00
2024-05-28 12:37:54 +00:00
- `json` — The JSON in which the field is searched for. [String ](../data-types/string.md#string )
- `field_name` — The name of the field to search for. [String literal ](../syntax#string )
2021-04-17 17:04:32 +00:00
2024-03-05 16:55:08 +00:00
**Returned value**
2017-12-28 15:13:23 +00:00
2024-05-28 11:43:22 +00:00
- Returns `1` if the field exists, `0` otherwise. [UInt8 ](../data-types/int-uint.md ).
2017-12-28 15:13:23 +00:00
2024-03-05 16:55:08 +00:00
**Example**
2017-12-28 15:13:23 +00:00
2024-03-05 16:55:08 +00:00
Query:
2021-04-17 17:04:32 +00:00
2024-03-05 16:55:08 +00:00
```sql
CREATE TABLE jsons
(
`json` String
)
ENGINE = Memory;
2017-12-28 15:13:23 +00:00
2024-03-05 16:55:08 +00:00
INSERT INTO jsons VALUES ('{"foo":"true","qux":1}');
SELECT simpleJSONHas(json, 'foo') FROM jsons;
SELECT simpleJSONHas(json, 'bar') FROM jsons;
```
2017-12-28 15:13:23 +00:00
2024-05-28 11:43:22 +00:00
Result:
2024-03-05 16:55:08 +00:00
```response
1
0
```
2024-05-28 12:48:10 +00:00
### simpleJSONExtractUInt
2021-04-17 17:04:32 +00:00
2024-03-05 16:55:08 +00:00
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` .
2017-12-28 15:13:23 +00:00
2024-03-05 16:55:08 +00:00
**Syntax**
2017-12-28 15:13:23 +00:00
2024-03-05 16:55:08 +00:00
```sql
simpleJSONExtractUInt(json, field_name)
```
2021-04-17 17:04:32 +00:00
2024-05-28 11:43:22 +00:00
Alias: `visitParamExtractUInt` .
2024-03-05 16:55:08 +00:00
**Parameters**
2017-12-28 15:13:23 +00:00
2024-05-28 12:37:54 +00:00
- `json` — The JSON in which the field is searched for. [String ](../data-types/string.md#string )
- `field_name` — The name of the field to search for. [String literal ](../syntax#string )
2017-12-28 15:13:23 +00:00
2024-03-05 16:55:08 +00:00
**Returned value**
2021-04-17 17:04:32 +00:00
2024-05-28 11:43:22 +00:00
- Returns the number parsed from the field if the field exists and contains a number, `0` otherwise. [UInt64 ](../data-types/int-uint.md ).
2017-12-28 15:13:23 +00:00
2024-03-05 16:55:08 +00:00
**Example**
Query:
```sql
CREATE TABLE jsons
(
`json` String
)
ENGINE = Memory;
INSERT INTO jsons VALUES ('{"foo":"4e3"}');
INSERT INTO jsons VALUES ('{"foo":3.4}');
INSERT INTO jsons VALUES ('{"foo":5}');
INSERT INTO jsons VALUES ('{"foo":"not1number"}');
INSERT INTO jsons VALUES ('{"baz":2}');
SELECT simpleJSONExtractUInt(json, 'foo') FROM jsons ORDER BY json;
2017-12-28 15:13:23 +00:00
```
2024-05-28 11:43:22 +00:00
Result:
2024-03-05 16:55:08 +00:00
```response
0
4
0
3
5
```
2017-12-28 15:13:23 +00:00
2024-05-28 12:48:10 +00:00
### simpleJSONExtractInt
2017-12-28 15:13:23 +00:00
2024-03-05 16:55:08 +00:00
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` .
2021-04-17 17:04:32 +00:00
2024-03-05 16:55:08 +00:00
**Syntax**
2017-12-28 15:13:23 +00:00
2024-03-05 16:55:08 +00:00
```sql
simpleJSONExtractInt(json, field_name)
2017-12-28 15:13:23 +00:00
```
2024-05-28 11:43:22 +00:00
Alias: `visitParamExtractInt` .
2024-03-05 16:55:08 +00:00
**Parameters**
2024-05-28 12:37:54 +00:00
- `json` — The JSON in which the field is searched for. [String ](../data-types/string.md#string )
- `field_name` — The name of the field to search for. [String literal ](../syntax#string )
2024-03-05 16:55:08 +00:00
**Returned value**
2024-05-28 11:43:22 +00:00
- Returns the number parsed from the field if the field exists and contains a number, `0` otherwise. [Int64 ](../data-types/int-uint.md ).
2024-03-05 16:55:08 +00:00
**Example**
Query:
```sql
CREATE TABLE jsons
(
`json` String
)
ENGINE = Memory;
INSERT INTO jsons VALUES ('{"foo":"-4e3"}');
INSERT INTO jsons VALUES ('{"foo":-3.4}');
INSERT INTO jsons VALUES ('{"foo":5}');
INSERT INTO jsons VALUES ('{"foo":"not1number"}');
INSERT INTO jsons VALUES ('{"baz":2}');
SELECT simpleJSONExtractInt(json, 'foo') FROM jsons ORDER BY json;
```
2024-05-28 11:43:22 +00:00
Result:
2024-03-05 16:55:08 +00:00
```response
0
-4
0
-3
5
```
2024-05-28 12:48:10 +00:00
### simpleJSONExtractFloat
2024-03-05 16:55:08 +00:00
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**
```sql
simpleJSONExtractFloat(json, field_name)
```
2024-05-28 11:43:22 +00:00
Alias: `visitParamExtractFloat` .
2024-03-05 16:55:08 +00:00
**Parameters**
2024-05-28 12:37:54 +00:00
- `json` — The JSON in which the field is searched for. [String ](../data-types/string.md#string )
- `field_name` — The name of the field to search for. [String literal ](../syntax#string )
2024-03-05 16:55:08 +00:00
**Returned value**
2024-05-28 11:43:22 +00:00
- Returns the number parsed from the field if the field exists and contains a number, `0` otherwise. [Float64 ](../data-types/float.md/#float32-float64 ).
2024-03-05 16:55:08 +00:00
**Example**
Query:
```sql
CREATE TABLE jsons
(
`json` String
)
ENGINE = Memory;
INSERT INTO jsons VALUES ('{"foo":"-4e3"}');
INSERT INTO jsons VALUES ('{"foo":-3.4}');
INSERT INTO jsons VALUES ('{"foo":5}');
INSERT INTO jsons VALUES ('{"foo":"not1number"}');
INSERT INTO jsons VALUES ('{"baz":2}');
SELECT simpleJSONExtractFloat(json, 'foo') FROM jsons ORDER BY json;
```
2024-05-28 11:43:22 +00:00
Result:
2024-03-05 16:55:08 +00:00
```response
0
-4000
0
-3.4
5
```
2024-05-28 12:48:10 +00:00
### simpleJSONExtractBool
2024-03-05 16:55:08 +00:00
Parses a true/false value from the value of the field named `field_name` . The result is `UInt8` .
**Syntax**
```sql
simpleJSONExtractBool(json, field_name)
```
2024-05-28 11:43:22 +00:00
Alias: `visitParamExtractBool` .
2024-03-05 16:55:08 +00:00
**Parameters**
2024-05-28 12:37:54 +00:00
- `json` — The JSON in which the field is searched for. [String ](../data-types/string.md#string )
- `field_name` — The name of the field to search for. [String literal ](../syntax#string )
2024-03-05 16:55:08 +00:00
**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:
```sql
CREATE TABLE jsons
(
`json` String
)
ENGINE = Memory;
INSERT INTO jsons VALUES ('{"foo":false,"bar":true}');
INSERT INTO jsons VALUES ('{"foo":"true","qux":1}');
SELECT simpleJSONExtractBool(json, 'bar') FROM jsons ORDER BY json;
SELECT simpleJSONExtractBool(json, 'foo') FROM jsons ORDER BY json;
```
2024-05-28 11:43:22 +00:00
Result:
2024-03-05 16:55:08 +00:00
```response
0
1
0
0
```
2024-05-28 12:48:10 +00:00
### simpleJSONExtractRaw
2024-03-05 16:55:08 +00:00
Returns the value of the field named `field_name` as a `String` , including separators.
**Syntax**
```sql
simpleJSONExtractRaw(json, field_name)
```
2024-05-28 11:43:22 +00:00
Alias: `visitParamExtractRaw` .
2024-03-05 16:55:08 +00:00
**Parameters**
2024-05-28 12:37:54 +00:00
- `json` — The JSON in which the field is searched for. [String ](../data-types/string.md#string )
- `field_name` — The name of the field to search for. [String literal ](../syntax#string )
2024-03-05 16:55:08 +00:00
**Returned value**
2024-05-28 11:43:22 +00:00
- Returns the value of the field as a string, including separators if the field exists, or an empty string otherwise. [`String` ](../data-types/string.md#string )
2024-03-05 16:55:08 +00:00
**Example**
Query:
```sql
CREATE TABLE jsons
(
`json` String
)
ENGINE = Memory;
INSERT INTO jsons VALUES ('{"foo":"-4e3"}');
INSERT INTO jsons VALUES ('{"foo":-3.4}');
INSERT INTO jsons VALUES ('{"foo":5}');
INSERT INTO jsons VALUES ('{"foo":{"def":[1,2,3]}}');
INSERT INTO jsons VALUES ('{"baz":2}');
SELECT simpleJSONExtractRaw(json, 'foo') FROM jsons ORDER BY json;
```
2024-05-28 11:43:22 +00:00
Result:
2024-03-05 16:55:08 +00:00
```response
"-4e3"
-3.4
5
{"def":[1,2,3]}
```
2024-05-28 12:48:10 +00:00
### simpleJSONExtractString
2024-03-05 16:55:08 +00:00
Parses `String` in double quotes from the value of the field named `field_name` .
**Syntax**
```sql
simpleJSONExtractString(json, field_name)
```
2024-05-28 11:43:22 +00:00
Alias: `visitParamExtractString` .
2024-03-05 16:55:08 +00:00
**Parameters**
2024-05-28 12:37:54 +00:00
- `json` — The JSON in which the field is searched for. [String ](../data-types/string.md#string )
- `field_name` — The name of the field to search for. [String literal ](../syntax#string )
2024-03-05 16:55:08 +00:00
**Returned value**
2024-05-28 11:43:22 +00:00
- 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 ](../data-types/string.md ).
2024-03-05 16:55:08 +00:00
**Implementation details**
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).
2024-03-05 16:55:08 +00:00
**Example**
Query:
```sql
CREATE TABLE jsons
(
`json` String
)
ENGINE = Memory;
INSERT INTO jsons VALUES ('{"foo":"\\n\\u0000"}');
INSERT INTO jsons VALUES ('{"foo":"\\u263"}');
INSERT INTO jsons VALUES ('{"foo":"\\u263a"}');
INSERT INTO jsons VALUES ('{"foo":"hello}');
SELECT simpleJSONExtractString(json, 'foo') FROM jsons ORDER BY json;
```
2024-05-28 11:43:22 +00:00
Result:
2024-03-05 16:55:08 +00:00
```response
\n\0
☺
```
2024-05-28 12:48:10 +00:00
## JSONExtract functions
2024-03-05 16:55:08 +00:00
2024-05-28 11:43:22 +00:00
The following functions are based on [simdjson ](https://github.com/lemire/simdjson ), and designed for more complex JSON parsing requirements.
2024-03-05 16:55:08 +00:00
2024-05-28 12:48:10 +00:00
### isValidJSON
2024-03-05 16:55:08 +00:00
2024-05-28 11:43:22 +00:00
Checks that passed string is valid JSON.
2024-03-05 16:55:08 +00:00
2024-05-28 12:37:54 +00:00
**Syntax**
```sql
isValidJSON(json)
```
2024-05-29 09:09:41 +00:00
**Examples**
2024-03-05 16:55:08 +00:00
2024-05-28 11:43:22 +00:00
``` sql
SELECT isValidJSON('{"a": "hello", "b": [-100, 200.0, 300]}') = 1
SELECT isValidJSON('not a json') = 0
```
2024-03-05 16:55:08 +00:00
2024-05-28 12:48:10 +00:00
### JSONHas
2024-03-05 16:55:08 +00:00
2024-05-28 11:43:22 +00:00
If the value exists in the JSON document, `1` will be returned. If the value does not exist, `0` will be returned.
2024-03-05 16:55:08 +00:00
2024-05-28 11:43:22 +00:00
**Syntax**
2024-03-05 16:55:08 +00:00
2024-05-28 11:43:22 +00:00
```sql
JSONHas(json [, indices_or_keys]...)
```
2024-03-05 16:55:08 +00:00
2024-05-28 11:43:22 +00:00
**Parameters**
2024-03-05 16:55:08 +00:00
2024-05-28 12:37:54 +00:00
- `json` — JSON string to parse. [String ](../data-types/string.md ).
2024-05-29 09:09:41 +00:00
- `indices_or_keys` — A list of zero or more arguments, each of which can be either string or integer. [String ](../data-types/string.md ), [Int* ](../data-types/int-uint.md ).
2024-03-05 16:55:08 +00:00
2024-05-28 11:43:22 +00:00
`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.
2024-03-05 16:55:08 +00:00
2024-05-28 11:43:22 +00:00
**Returned value**
2024-03-05 16:55:08 +00:00
2024-05-28 11:43:22 +00:00
- Returns `1` if the value exists in `json` , otherwise `0` . [UInt8 ](../data-types/int-uint.md ).
2023-02-14 14:35:25 +00:00
2024-05-28 11:43:22 +00:00
**Examples**
2023-02-14 14:45:45 +00:00
2024-05-28 11:43:22 +00:00
Query:
2019-10-12 12:00:46 +00:00
2024-05-28 11:43:22 +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-10-12 12:00:46 +00:00
2024-05-28 11:43:22 +00:00
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:
2019-10-12 12:00:46 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2024-05-28 11:43:22 +00:00
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-10-12 12:00:46 +00:00
```
2024-05-28 12:48:10 +00:00
### JSONLength
2019-04-24 04:23:14 +00:00
2024-05-28 11:43:22 +00:00
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.
2019-04-24 04:23:14 +00:00
2024-05-28 11:43:22 +00:00
**Syntax**
2019-04-24 04:23:14 +00:00
2024-05-28 11:43:22 +00:00
```sql
JSONLength(json [, indices_or_keys]...)
```
**Parameters**
2024-05-28 12:37:54 +00:00
- `json` — JSON string to parse. [String ](../data-types/string.md ).
2024-05-29 09:09:41 +00:00
- `indices_or_keys` — A list of zero or more arguments, each of which can be either string or integer. [String ](../data-types/string.md ), [Int* ](../data-types/int-uint.md ).
2024-05-28 11:43:22 +00:00
`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 ](../data-types/int-uint.md ).
**Examples**
2019-04-24 04:23:14 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2024-05-28 11:43:22 +00:00
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
```
2024-05-28 12:48:10 +00:00
### JSONType
2019-04-24 04:23:14 +00:00
2024-11-21 14:03:38 +00:00
Return the type of a JSON value. If the value does not exist, `Null` will be returned (not usual [Null ](../data-types/nullable.md ), but a special Null=0 of `Enum8('Null' = 0, 'String' = 34,...` ). .
2024-05-28 11:43:22 +00:00
**Syntax**
```sql
JSONType(json [, indices_or_keys]...)
```
**Parameters**
2024-05-28 12:37:54 +00:00
- `json` — JSON string to parse. [String ](../data-types/string.md ).
2024-05-29 09:09:41 +00:00
- `indices_or_keys` — A list of zero or more arguments, each of which can be either string or integer. [String ](../data-types/string.md ), [Int* ](../data-types/int-uint.md ).
2024-05-28 11:43:22 +00:00
`indices_or_keys` type:
2023-04-19 15:55:29 +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-04-24 04:23:14 +00:00
2024-05-28 11:43:22 +00:00
**Returned value**
2019-06-14 12:44:33 +00:00
2024-11-21 14:03:38 +00:00
- Returns the type of a JSON value as a string, otherwise if the value doesn't exists it returns `Null=0` . [Enum ](../data-types/enum.md ).
2019-04-24 04:23:14 +00:00
2024-05-28 11:43:22 +00:00
**Examples**
2019-04-24 04:23:14 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2024-05-28 11:43:22 +00:00
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
```
2024-05-28 12:48:10 +00:00
### JSONExtractUInt
2019-04-24 04:23:14 +00:00
2024-05-28 11:43:22 +00:00
Parses JSON and extracts a value of UInt type.
2019-04-24 04:23:14 +00:00
2024-05-28 11:43:22 +00:00
**Syntax**
2019-04-24 04:23:14 +00:00
2024-05-28 11:43:22 +00:00
```sql
JSONExtractUInt(json [, indices_or_keys]...)
```
**Parameters**
2024-05-28 12:37:54 +00:00
- `json` — JSON string to parse. [String ](../data-types/string.md ).
2024-05-29 09:09:41 +00:00
- `indices_or_keys` — A list of zero or more arguments, each of which can be either string or integer. [String ](../data-types/string.md ), [Int* ](../data-types/int-uint.md ).
2024-05-28 11:43:22 +00:00
`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**
2024-11-21 13:54:57 +00:00
- Returns a UInt value if it exists, otherwise it returns `0` . [UInt64 ](../data-types/int-uint.md ).
2024-05-28 11:43:22 +00:00
**Examples**
Query:
2019-04-24 04:23:14 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2024-05-28 11:43:22 +00:00
SELECT JSONExtractUInt('{"a": "hello", "b": [-100, 200.0, 300]}', 'b', -1) as x, toTypeName(x);
2019-04-24 04:23:14 +00:00
```
2024-05-28 11:43:22 +00:00
Result:
2019-04-24 04:23:14 +00:00
2024-05-28 11:43:22 +00:00
```response
┌───x─┬─toTypeName(x)─┐
│ 300 │ UInt64 │
└─────┴───────────────┘
```
2019-04-24 04:23:14 +00:00
2024-05-28 12:48:10 +00:00
### JSONExtractInt
2019-04-24 04:23:14 +00:00
2024-05-28 11:43:22 +00:00
Parses JSON and extracts a value of Int type.
**Syntax**
```sql
JSONExtractInt(json [, indices_or_keys]...)
```
**Parameters**
2024-05-28 12:37:54 +00:00
- `json` — JSON string to parse. [String ](../data-types/string.md ).
2024-05-29 09:09:41 +00:00
- `indices_or_keys` — A list of zero or more arguments, each of which can be either string or integer. [String ](../data-types/string.md ), [Int* ](../data-types/int-uint.md ).
2024-05-28 11:43:22 +00:00
`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**
2024-11-21 13:54:57 +00:00
- Returns an Int value if it exists, otherwise it returns `0` . [Int64 ](../data-types/int-uint.md ).
2024-05-28 11:43:22 +00:00
**Examples**
Query:
2019-04-24 04:23:14 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2024-05-28 11:43:22 +00:00
SELECT JSONExtractInt('{"a": "hello", "b": [-100, 200.0, 300]}', 'b', -1) as x, toTypeName(x);
2019-04-24 04:23:14 +00:00
```
2024-05-28 11:43:22 +00:00
Result:
```response
┌───x─┬─toTypeName(x)─┐
│ 300 │ Int64 │
└─────┴───────────────┘
```
2020-03-20 10:10:48 +00:00
2024-05-28 12:48:10 +00:00
### JSONExtractFloat
2020-03-20 10:10:48 +00:00
2024-05-28 11:43:22 +00:00
Parses JSON and extracts a value of Int type.
2020-03-20 10:10:48 +00:00
2024-05-28 11:43:22 +00:00
**Syntax**
2019-04-24 04:23:14 +00:00
2024-05-28 11:43:22 +00:00
```sql
JSONExtractFloat(json [, indices_or_keys]...)
```
2019-04-24 04:23:14 +00:00
2024-05-28 11:43:22 +00:00
**Parameters**
2019-04-24 04:23:14 +00:00
2024-05-28 12:37:54 +00:00
- `json` — JSON string to parse. [String ](../data-types/string.md ).
2024-05-29 09:09:41 +00:00
- `indices_or_keys` — A list of zero or more arguments, each of which can be either string or integer. [String ](../data-types/string.md ), [Int* ](../data-types/int-uint.md ).
2024-05-28 11:43:22 +00:00
`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**
2024-11-21 13:54:57 +00:00
- Returns an Float value if it exists, otherwise it returns `0` . [Float64 ](../data-types/float.md ).
2024-05-28 11:43:22 +00:00
**Examples**
Query:
2019-04-24 04:23:14 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2024-05-28 11:43:22 +00:00
SELECT JSONExtractFloat('{"a": "hello", "b": [-100, 200.0, 300]}', 'b', 2) as x, toTypeName(x);
2019-04-24 04:23:14 +00:00
```
2024-05-28 11:43:22 +00:00
Result:
2019-04-24 04:23:14 +00:00
2024-05-28 11:43:22 +00:00
```response
┌───x─┬─toTypeName(x)─┐
│ 200 │ Float64 │
└─────┴───────────────┘
```
2019-04-24 04:23:14 +00:00
2024-05-28 12:48:10 +00:00
### JSONExtractBool
2019-04-24 04:23:14 +00:00
2024-05-28 11:43:22 +00:00
Parses JSON and extracts a boolean value. If the value does not exist or has a wrong type, `0` will be returned.
2019-04-24 04:23:14 +00:00
2024-05-28 11:43:22 +00:00
**Syntax**
```sql
JSONExtractBool(json\[, indices_or_keys\]...)
```
**Parameters**
2024-05-28 12:37:54 +00:00
- `json` — JSON string to parse. [String ](../data-types/string.md ).
2024-05-29 09:09:41 +00:00
- `indices_or_keys` — A list of zero or more arguments, each of which can be either string or integer. [String ](../data-types/string.md ), [Int* ](../data-types/int-uint.md ).
2024-05-28 11:43:22 +00:00
`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**
2024-05-28 12:37:54 +00:00
- Returns a Boolean value if it exists, otherwise it returns `0` . [Bool ](../data-types/boolean.md ).
2024-05-28 11:43:22 +00:00
**Example**
Query:
``` sql
SELECT JSONExtractBool('{"passed": true}', 'passed');
```
Result:
```response
┌─JSONExtractBool('{"passed": true}', 'passed')─┐
│ 1 │
└───────────────────────────────────────────────┘
```
2024-05-28 12:48:10 +00:00
### JSONExtractString
2024-05-28 11:43:22 +00:00
Parses JSON and extracts a string. This function is similar to [`visitParamExtractString` ](#simplejsonextractstring ) functions. If the value does not exist or has a wrong type, an empty string will be returned.
**Syntax**
```sql
JSONExtractString(json [, indices_or_keys]...)
```
**Parameters**
2024-05-28 12:37:54 +00:00
- `json` — JSON string to parse. [String ](../data-types/string.md ).
2024-05-29 09:09:41 +00:00
- `indices_or_keys` — A list of zero or more arguments, each of which can be either string or integer. [String ](../data-types/string.md ), [Int* ](../data-types/int-uint.md ).
2024-05-28 11:43:22 +00:00
`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**
2024-05-29 11:52:47 +00:00
- 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 ](../data-types/string.md ).
2024-05-28 11:43:22 +00:00
**Examples**
2019-04-24 04:23:14 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-09-23 15:31:46 +00:00
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
```
2024-05-28 12:48:10 +00:00
### JSONExtract
2019-05-13 13:46:19 +00:00
2024-05-28 12:37:54 +00:00
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:
2019-05-13 13:46:19 +00:00
2019-05-16 13:35:31 +00:00
`JSONExtract(..., 'String')` returns exactly the same as `JSONExtractString()` ,
`JSONExtract(..., 'Float64')` returns exactly the same as `JSONExtractFloat()` .
2019-05-13 13:46:19 +00:00
2024-05-28 12:37:54 +00:00
**Syntax**
```sql
JSONExtract(json [, indices_or_keys...], return_type)
```
**Parameters**
- `json` — JSON string to parse. [String ](../data-types/string.md ).
2024-05-29 09:09:41 +00:00
- `indices_or_keys` — A list of zero or more arguments, each of which can be either string or integer. [String ](../data-types/string.md ), [Int* ](../data-types/int-uint.md ).
2024-05-28 12:37:54 +00:00
- `return_type` — A string specifying the type of the value to extract. [String ](../data-types/string.md ).
`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 ](../data-types/int-uint.md ), [Int64 ](../data-types/int-uint.md ), [Float64 ](../data-types/float.md ), [Bool ](../data-types/boolean.md ) or [String ](../data-types/string.md ).
**Examples**
2019-05-13 13:46:19 +00:00
2020-03-20 10:10:48 +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')
2023-04-12 02:18:07 +00:00
SELECT JSONExtract('{"a": "hello", "b": "world"}', 'Map(String, String)') = map('a', 'hello', 'b', 'world');
2019-05-16 13:35:31 +00:00
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'
```
2024-05-28 12:48:10 +00:00
### JSONExtractKeysAndValues
2024-05-28 12:37:54 +00:00
Parses key-value pairs from JSON where the values are of the given ClickHouse data type.
**Syntax**
```sql
JSONExtractKeysAndValues(json [, indices_or_keys...], value_type)
```
**Parameters**
- `json` — JSON string to parse. [String ](../data-types/string.md ).
2024-05-29 09:09:41 +00:00
- `indices_or_keys` — A list of zero or more arguments, each of which can be either string or integer. [String ](../data-types/string.md ), [Int* ](../data-types/int-uint.md ).
2024-05-28 12:37:54 +00:00
- `value_type` — A string specifying the type of the value to extract. [String ](../data-types/string.md ).
`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.
2019-05-16 13:35:31 +00:00
2024-05-28 12:37:54 +00:00
**Returned value**
2019-05-16 13:35:31 +00:00
2024-05-28 12:37:54 +00:00
- Returns an array of parsed key-value pairs. [Array ](../data-types/array.md )([Tuple](../data-types/tuple.md)(`value_type`)).
**Example**
2019-05-16 13:35:31 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +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
```
2024-05-28 12:48:10 +00:00
### JSONExtractKeys
2021-11-02 17:01:49 +00:00
2021-11-06 20:58:06 +00:00
Parses a JSON string and extracts the keys.
2021-11-02 17:01:49 +00:00
**Syntax**
``` sql
2021-11-06 20:58:15 +00:00
JSONExtractKeys(json[, a, b, c...])
2021-11-02 17:01:49 +00:00
```
2024-05-28 11:43:22 +00:00
**Parameters**
2021-11-02 17:01:49 +00:00
2024-05-24 03:54:16 +00:00
- `json` — [String ](../data-types/string.md ) 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 ](../data-types/string.md ) to get the field by the key or an [Integer ](../data-types/int-uint.md ) 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.
2021-11-02 17:01:49 +00:00
**Returned value**
2024-05-28 12:37:54 +00:00
- Returns an array with the keys of the JSON. [Array ](../data-types/array.md )([String](../data-types/string.md)).
2021-11-02 17:01:49 +00:00
**Example**
Query:
```sql
SELECT JSONExtractKeys('{"a": "hello", "b": [-100, 200.0, 300]}');
```
Result:
```
text
┌─JSONExtractKeys('{"a": "hello", "b": [-100, 200.0, 300]}')─┐
│ ['a','b'] │
└────────────────────────────────────────────────────────────┘
```
2024-05-28 12:48:10 +00:00
### JSONExtractRaw
2019-05-16 13:35:31 +00:00
2024-05-28 11:43:22 +00:00
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.
2019-05-16 13:35:31 +00:00
2024-05-28 11:43:22 +00:00
**Syntax**
2019-05-16 13:35:31 +00:00
2024-05-28 11:43:22 +00:00
```sql
JSONExtractRaw(json [, indices_or_keys]...)
```
**Parameters**
2024-05-28 12:37:54 +00:00
- `json` — JSON string to parse. [String ](../data-types/string.md ).
2024-05-29 09:09:41 +00:00
- `indices_or_keys` — A list of zero or more arguments, each of which can be either string or integer. [String ](../data-types/string.md ), [Int* ](../data-types/int-uint.md ).
2024-05-28 11:43:22 +00:00
`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**
2024-05-29 09:09:41 +00:00
- 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 ](../data-types/string.md ).
2024-05-28 11:43:22 +00:00
**Example**
2019-05-16 13:35:31 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
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
2024-05-28 12:48:10 +00:00
### JSONExtractArrayRaw
2019-12-08 14:49:26 +00:00
2024-05-29 09:09:41 +00:00
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.
2019-12-08 14:49:26 +00:00
2024-05-28 11:43:22 +00:00
**Syntax**
2019-12-08 14:49:26 +00:00
2024-05-28 11:43:22 +00:00
```sql
JSONExtractArrayRaw(json [, indices_or_keys...])
```
2019-12-08 14:49:26 +00:00
2024-05-28 11:43:22 +00:00
**Parameters**
2024-05-28 12:37:54 +00:00
- `json` — JSON string to parse. [String ](../data-types/string.md ).
2024-05-29 09:09:41 +00:00
- `indices_or_keys` — A list of zero or more arguments, each of which can be either string or integer. [String ](../data-types/string.md ), [Int* ](../data-types/int-uint.md ).
2024-05-28 11:43:22 +00:00
`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 ](../data-types/array.md )([String](../data-types/string.md)).
**Example**
```sql
2021-11-25 21:20:37 +00:00
SELECT JSONExtractArrayRaw('{"a": "hello", "b": [-100, 200.0, "hello"]}', 'b') = ['-100', '200.0', '"hello"'];
2019-12-08 14:49:26 +00:00
```
2024-05-28 12:48:10 +00:00
### JSONExtractKeysAndValuesRaw
2020-04-20 10:08:39 +00:00
2020-05-07 18:38:27 +00:00
Extracts raw data from a JSON object.
2020-04-20 10:08:39 +00:00
2020-05-07 18:38:27 +00:00
**Syntax**
2020-04-20 10:08:39 +00:00
2020-05-07 18:38:27 +00:00
``` sql
JSONExtractKeysAndValuesRaw(json[, p, a, t, h])
```
2021-02-15 21:22:10 +00:00
**Arguments**
2020-05-07 18:38:27 +00:00
2024-05-24 03:54:16 +00:00
- `json` — [String ](../data-types/string.md ) 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 ](../data-types/string.md ) to get the field by the key or an [integer ](../data-types/int-uint.md ) 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.
2020-05-07 18:38:27 +00:00
**Returned values**
2024-05-24 03:54:16 +00:00
- Array with `('key', 'value')` tuples. Both tuple members are strings. [Array ](../data-types/array.md )([Tuple](../data-types/tuple.md)([String](../data-types/string.md), [String ](../data-types/string.md )).
- Empty array if the requested object does not exist, or input JSON is invalid. [Array ](../data-types/array.md )([Tuple](../data-types/tuple.md)([String](../data-types/string.md), [String ](../data-types/string.md )).
2020-05-07 18:38:27 +00:00
**Examples**
Query:
2020-04-20 10:08:39 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}');
2020-05-07 18:38:27 +00:00
```
Result:
``` text
┌─JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}')─┐
│ [('a','[-100,200]'),('b','{"c":{"d":"hello","f":"world"}}')] │
└──────────────────────────────────────────────────────────────────────────────────────────────┘
2020-04-20 10:08:39 +00:00
```
2020-05-07 18:38:27 +00:00
Query:
``` sql
2021-03-13 18:18:45 +00:00
SELECT JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}', 'b');
2020-05-07 18:38:27 +00:00
```
Result:
``` text
┌─JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}', 'b')─┐
│ [('c','{"d":"hello","f":"world"}')] │
└───────────────────────────────────────────────────────────────────────────────────────────────────┘
```
Query:
``` sql
2021-03-13 18:18:45 +00:00
SELECT JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}', -1, 'c');
2020-05-07 18:38:27 +00:00
```
Result:
``` text
┌─JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}', -1, 'c')─┐
│ [('d','"hello"'),('f','"world"')] │
└───────────────────────────────────────────────────────────────────────────────────────────────────────┘
```
2024-05-28 12:48:10 +00:00
### JSON_EXISTS
2021-10-21 13:39:10 +00:00
2024-05-28 11:43:22 +00:00
If the value exists in the JSON document, `1` will be returned. If the value does not exist, `0` will be returned.
2021-10-21 13:39:10 +00:00
2024-05-28 11:43:22 +00:00
**Syntax**
2021-10-21 13:39:10 +00:00
2024-05-28 11:43:22 +00:00
```sql
JSON_EXISTS(json, path)
```
**Parameters**
2024-05-28 12:37:54 +00:00
- `json` — A string with valid JSON. [String ](../data-types/string.md ).
- `path` — A string representing the path. [String ](../data-types/string.md ).
2024-05-28 11:43:22 +00:00
:::note
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, otherwise `0` .
**Examples**
2021-10-21 13:39:10 +00:00
``` sql
2021-10-23 16:04:29 +00:00
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]');
2021-10-21 13:39:10 +00:00
```
2024-05-28 12:48:10 +00:00
### JSON_QUERY
2024-05-28 11:43:22 +00:00
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**
```sql
JSON_QUERY(json, path)
```
**Parameters**
2024-05-28 12:37:54 +00:00
- `json` — A string with valid JSON. [String ](../data-types/string.md ).
- `path` — A string representing the path. [String ](../data-types/string.md ).
2024-05-28 11:43:22 +00:00
2023-10-25 15:08:27 +00:00
:::note
2022-04-09 13:29:05 +00:00
Before version 21.11 the order of arguments was wrong, i.e. JSON_EXISTS(path, json)
:::
2021-10-21 13:39:10 +00:00
2024-05-28 11:43:22 +00:00
**Returned value**
2021-10-21 13:39:10 +00:00
2024-05-28 11:43:22 +00:00
- Returns the extracted value as a JSON array or JSON object. Otherwise it returns an empty string if the value does not exist. [String ](../data-types/string.md ).
2021-10-21 13:39:10 +00:00
2024-05-28 11:43:22 +00:00
**Example**
2021-10-21 13:39:10 +00:00
2024-05-28 11:43:22 +00:00
Query:
2021-10-21 13:39:10 +00:00
``` sql
2021-10-23 16:04:29 +00:00
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'));
2021-10-21 13:39:10 +00:00
```
Result:
``` text
["world"]
[0, 1, 4, 0, -1, -4]
[2]
String
```
2024-05-28 11:43:22 +00:00
2024-05-28 12:48:10 +00:00
### JSON_VALUE
2024-05-28 11:43:22 +00:00
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**
```sql
JSON_VALUE(json, path)
```
**Parameters**
2024-05-28 12:37:54 +00:00
- `json` — A string with valid JSON. [String ](../data-types/string.md ).
- `path` — A string representing the path. [String ](../data-types/string.md ).
2024-05-28 11:43:22 +00:00
2023-10-25 15:08:27 +00:00
:::note
2024-05-28 11:43:22 +00:00
Before version 21.11 the order of arguments was wrong, i.e. JSON_EXISTS(path, json)
2022-04-09 13:29:05 +00:00
:::
2021-10-21 13:39:10 +00:00
2024-05-28 11:43:22 +00:00
**Returned value**
2021-10-21 13:39:10 +00:00
2024-05-28 11:43:22 +00:00
- Returns the extracted value as a JSON scalar if it exists, otherwise an empty string is returned. [String ](../data-types/string.md ).
2021-10-21 13:39:10 +00:00
2024-05-28 11:43:22 +00:00
**Example**
2021-10-21 13:39:10 +00:00
2024-05-28 11:43:22 +00:00
Query:
2021-10-21 13:39:10 +00:00
``` sql
2021-10-23 16:04:29 +00:00
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'));
2023-10-26 17:26:18 +00:00
select JSON_VALUE('{"hello":"world"}', '$.b') settings function_json_value_return_type_allow_nullable=true;
2023-03-25 13:54:45 +00:00
select JSON_VALUE('{"hello":{"world":"!"}}', '$.hello') settings function_json_value_return_type_allow_complex=true;
2021-10-21 13:39:10 +00:00
```
Result:
``` text
2022-04-15 11:03:59 +00:00
world
2021-10-21 13:39:10 +00:00
0
2
String
```
2024-05-28 12:48:10 +00:00
### toJSONString
2021-07-01 20:57:01 +00:00
2021-07-06 21:01:45 +00:00
Serializes a value to its JSON representation. Various data types and nested structures are supported.
2024-05-24 03:54:16 +00:00
64-bit [integers ](../data-types/int-uint.md ) or bigger (like `UInt64` or `Int128` ) are enclosed in quotes by default. [output_format_json_quote_64bit_integers ](../../operations/settings/settings.md#session_settings-output_format_json_quote_64bit_integers ) controls this behavior.
2021-07-10 07:01:34 +00:00
Special values `NaN` and `inf` are replaced with `null` . Enable [output_format_json_quote_denormals ](../../operations/settings/settings.md#settings-output_format_json_quote_denormals ) setting to show them.
2024-05-24 03:54:16 +00:00
When serializing an [Enum ](../data-types/enum.md ) value, the function outputs its name.
2021-07-01 20:57:01 +00:00
**Syntax**
``` sql
toJSONString(value)
```
**Arguments**
2023-04-19 15:55:29 +00:00
- `value` — Value to serialize. Value may be of any data type.
2021-07-01 20:57:01 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- JSON representation of the value. [String ](../data-types/string.md ).
2021-07-01 20:57:01 +00:00
**Example**
2024-05-24 03:54:16 +00:00
The first example shows serialization of a [Map ](../data-types/map.md ).
The second example shows some special values wrapped into a [Tuple ](../data-types/tuple.md ).
2021-07-06 20:49:14 +00:00
2021-07-01 20:57:01 +00:00
Query:
``` sql
2021-07-06 20:49:14 +00:00
SELECT toJSONString(map('key1', 1, 'key2', 2));
2021-07-10 07:01:34 +00:00
SELECT toJSONString(tuple(1.25, NULL, NaN, +inf, -inf, [])) SETTINGS output_format_json_quote_denormals = 1;
2021-07-01 20:57:01 +00:00
```
Result:
``` text
2021-07-06 20:49:14 +00:00
{"key1":1,"key2":2}
2021-07-10 07:01:34 +00:00
[1.25,null,"nan","inf","-inf",[]]
2021-07-01 20:57:01 +00:00
```
2021-07-08 20:18:56 +00:00
**See Also**
2023-04-19 15:55:29 +00:00
- [output_format_json_quote_64bit_integers ](../../operations/settings/settings.md#session_settings-output_format_json_quote_64bit_integers )
- [output_format_json_quote_denormals ](../../operations/settings/settings.md#settings-output_format_json_quote_denormals )
2023-02-21 03:17:44 +00:00
2024-05-28 12:48:10 +00:00
### JSONArrayLength
2023-02-21 03:17:44 +00:00
Returns the number of elements in the outermost JSON array. The function returns NULL if input JSON string is invalid.
**Syntax**
``` sql
JSONArrayLength(json)
```
Alias: `JSON_ARRAY_LENGTH(json)` .
**Arguments**
2024-05-24 03:54:16 +00:00
- `json` — [String ](../data-types/string.md ) with valid JSON.
2023-02-21 03:17:44 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- If `json` is a valid JSON array string, returns the number of array elements, otherwise returns NULL. [Nullable(UInt64) ](../data-types/int-uint.md ).
2023-02-21 03:17:44 +00:00
**Example**
``` sql
SELECT
JSONArrayLength(''),
JSONArrayLength('[1,2,3]')
┌─JSONArrayLength('')─┬─JSONArrayLength('[1,2,3]')─┐
│ ᴺᵁᴸᴸ │ 3 │
└─────────────────────┴────────────────────────────┘
```
2023-09-07 02:10:33 +00:00
2024-05-28 12:48:10 +00:00
### jsonMergePatch
2023-09-07 02:10:33 +00:00
2023-10-25 15:08:27 +00:00
Returns the merged JSON object string which is formed by merging multiple JSON objects.
2023-09-07 02:10:33 +00:00
**Syntax**
``` sql
2023-10-10 06:51:51 +00:00
jsonMergePatch(json1, json2, ...)
2023-09-07 02:10:33 +00:00
```
**Arguments**
2024-05-24 03:54:16 +00:00
- `json` — [String ](../data-types/string.md ) with valid JSON.
2023-09-07 02:10:33 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- If JSON object strings are valid, return the merged JSON object string. [String ](../data-types/string.md ).
2023-09-07 02:10:33 +00:00
**Example**
``` sql
2023-10-10 06:51:51 +00:00
SELECT jsonMergePatch('{"a":1}', '{"name": "joey"}', '{"name": "tom"}', '{"name": "zoey"}') AS res
2023-09-07 02:10:33 +00:00
┌─res───────────────────┐
│ {"a":1,"name":"zoey"} │
└───────────────────────┘
```
2024-07-20 18:51:20 +00:00
### JSONAllPaths
2024-07-23 10:10:15 +00:00
Returns the list of all paths stored in each row in [JSON ](../data-types/newjson.md ) column.
2024-07-20 18:51:20 +00:00
**Syntax**
``` sql
JSONAllPaths(json)
```
**Arguments**
2024-07-23 10:10:15 +00:00
- `json` — [JSON ](../data-types/newjson.md ).
2024-07-20 18:51:20 +00:00
**Returned value**
- An array of paths. [Array(String) ](../data-types/array.md ).
**Example**
``` sql
CREATE TABLE test (json JSON(max_dynamic_paths=1)) ENGINE = Memory;
INSERT INTO test FORMAT JSONEachRow {"json" : {"a" : 42}}, {"json" : {"b" : "Hello"}}, {"json" : {"a" : [1, 2, 3], "c" : "2020-01-01"}}
SELECT json, JSONAllPaths(json) FROM test;
```
```text
┌─json─────────────────────────────────┬─JSONAllPaths(json)─┐
│ {"a":"42"} │ ['a'] │
│ {"b":"Hello"} │ ['b'] │
│ {"a":["1","2","3"],"c":"2020-01-01"} │ ['a','c'] │
└──────────────────────────────────────┴────────────────────┘
```
### JSONAllPathsWithTypes
2024-07-23 10:10:15 +00:00
Returns the map of all paths and their data types stored in each row in [JSON ](../data-types/newjson.md ) column.
2024-07-20 18:51:20 +00:00
**Syntax**
``` sql
JSONAllPathsWithTypes(json)
```
**Arguments**
2024-07-23 10:10:15 +00:00
- `json` — [JSON ](../data-types/newjson.md ).
2024-07-20 18:51:20 +00:00
**Returned value**
- An array of paths. [Map(String, String) ](../data-types/array.md ).
**Example**
``` sql
CREATE TABLE test (json JSON(max_dynamic_paths=1)) ENGINE = Memory;
INSERT INTO test FORMAT JSONEachRow {"json" : {"a" : 42}}, {"json" : {"b" : "Hello"}}, {"json" : {"a" : [1, 2, 3], "c" : "2020-01-01"}}
SELECT json, JSONAllPathsWithTypes(json) FROM test;
```
```text
┌─json─────────────────────────────────┬─JSONAllPathsWithTypes(json)───────────────┐
│ {"a":"42"} │ {'a':'Int64'} │
│ {"b":"Hello"} │ {'b':'String'} │
│ {"a":["1","2","3"],"c":"2020-01-01"} │ {'a':'Array(Nullable(Int64))','c':'Date'} │
└──────────────────────────────────────┴───────────────────────────────────────────┘
```
### JSONDynamicPaths
2024-07-23 10:10:15 +00:00
Returns the list of dynamic paths that are stored as separate subcolumns in [JSON ](../data-types/newjson.md ) column.
2024-07-20 18:51:20 +00:00
**Syntax**
``` sql
JSONDynamicPaths(json)
```
**Arguments**
2024-07-23 10:10:15 +00:00
- `json` — [JSON ](../data-types/newjson.md ).
2024-07-20 18:51:20 +00:00
**Returned value**
- An array of paths. [Array(String) ](../data-types/array.md ).
**Example**
``` sql
CREATE TABLE test (json JSON(max_dynamic_paths=1)) ENGINE = Memory;
INSERT INTO test FORMAT JSONEachRow {"json" : {"a" : 42}}, {"json" : {"b" : "Hello"}}, {"json" : {"a" : [1, 2, 3], "c" : "2020-01-01"}}
SELECT json, JSONDynamicPaths(json) FROM test;
```
```text
┌─json─────────────────────────────────┬─JSONDynamicPaths(json)─┐
| {"a":"42"} │ ['a'] │
│ {"b":"Hello"} │ [] │
│ {"a":["1","2","3"],"c":"2020-01-01"} │ ['a'] │
└──────────────────────────────────────┴────────────────────────┘
```
### JSONDynamicPathsWithTypes
2024-07-23 10:10:15 +00:00
Returns the map of dynamic paths that are stored as separate subcolumns and their types in each row in [JSON ](../data-types/newjson.md ) column.
2024-07-20 18:51:20 +00:00
**Syntax**
``` sql
JSONAllPathsWithTypes(json)
```
**Arguments**
2024-07-23 10:10:15 +00:00
- `json` — [JSON ](../data-types/newjson.md ).
2024-07-20 18:51:20 +00:00
**Returned value**
- An array of paths. [Map(String, String) ](../data-types/array.md ).
**Example**
``` sql
CREATE TABLE test (json JSON(max_dynamic_paths=1)) ENGINE = Memory;
INSERT INTO test FORMAT JSONEachRow {"json" : {"a" : 42}}, {"json" : {"b" : "Hello"}}, {"json" : {"a" : [1, 2, 3], "c" : "2020-01-01"}}
SELECT json, JSONDynamicPathsWithTypes(json) FROM test;
```
```text
┌─json─────────────────────────────────┬─JSONDynamicPathsWithTypes(json)─┐
│ {"a":"42"} │ {'a':'Int64'} │
│ {"b":"Hello"} │ {} │
│ {"a":["1","2","3"],"c":"2020-01-01"} │ {'a':'Array(Nullable(Int64))'} │
└──────────────────────────────────────┴─────────────────────────────────┘
```
### JSONSharedDataPaths
2024-07-23 10:10:15 +00:00
Returns the list of paths that are stored in shared data structure in [JSON ](../data-types/newjson.md ) column.
2024-07-20 18:51:20 +00:00
**Syntax**
``` sql
JSONSharedDataPaths(json)
```
**Arguments**
2024-07-23 10:10:15 +00:00
- `json` — [JSON ](../data-types/newjson.md ).
2024-07-20 18:51:20 +00:00
**Returned value**
- An array of paths. [Array(String) ](../data-types/array.md ).
**Example**
``` sql
CREATE TABLE test (json JSON(max_dynamic_paths=1)) ENGINE = Memory;
INSERT INTO test FORMAT JSONEachRow {"json" : {"a" : 42}}, {"json" : {"b" : "Hello"}}, {"json" : {"a" : [1, 2, 3], "c" : "2020-01-01"}}
SELECT json, JSONSharedDataPaths(json) FROM test;
```
```text
┌─json─────────────────────────────────┬─JSONSharedDataPaths(json)─┐
│ {"a":"42"} │ [] │
│ {"b":"Hello"} │ ['b'] │
│ {"a":["1","2","3"],"c":"2020-01-01"} │ ['c'] │
└──────────────────────────────────────┴───────────────────────────┘
```
### JSONSharedDataPathsWithTypes
2024-07-23 10:10:15 +00:00
Returns the map of paths that are stored in shared data structure and their types in each row in [JSON ](../data-types/newjson.md ) column.
2024-07-20 18:51:20 +00:00
**Syntax**
``` sql
JSONSharedDataPathsWithTypes(json)
```
**Arguments**
2024-07-23 10:10:15 +00:00
- `json` — [JSON ](../data-types/newjson.md ).
2024-07-20 18:51:20 +00:00
**Returned value**
- An array of paths. [Map(String, String) ](../data-types/array.md ).
**Example**
``` sql
CREATE TABLE test (json JSON(max_dynamic_paths=1)) ENGINE = Memory;
INSERT INTO test FORMAT JSONEachRow {"json" : {"a" : 42}}, {"json" : {"b" : "Hello"}}, {"json" : {"a" : [1, 2, 3], "c" : "2020-01-01"}}
SELECT json, JSONSharedDataPathsWithTypes(json) FROM test;
```
```text
┌─json─────────────────────────────────┬─JSONSharedDataPathsWithTypes(json)─┐
│ {"a":"42"} │ {} │
│ {"b":"Hello"} │ {'b':'String'} │
│ {"a":["1","2","3"],"c":"2020-01-01"} │ {'c':'Date'} │
└──────────────────────────────────────┴────────────────────────────────────┘
```