Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #30450 from morozovsk/patch-10
Documentation: JSON_EXISTS/JSON_QUERY/JSON_VALUE
  • Loading branch information
kssenii committed Oct 24, 2021
2 parents 855b102 + 37ee06d commit cd63b24
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
71 changes: 71 additions & 0 deletions docs/en/sql-reference/functions/json-functions.md
Expand Up @@ -306,6 +306,77 @@ Result:
└───────────────────────────────────────────────────────────────────────────────────────────────────────┘
```
## JSON_EXISTS(json, path) {#json-exists}
If the value exists in the JSON document, `1` will be returned.
If the value does not exist, `0` will be returned.
Examples:
``` sql
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]');
```
!!! note "Note"
before version 21.11 the order of arguments was wrong, i.e. JSON_EXISTS(path, json)
## JSON_QUERY(json, path) {#json-query}
Parses a JSON and extract a value as JSON array or JSON object.
If the value does not exist, an empty string will be returned.
Example:
``` sql
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:
``` text
["world"]
[0, 1, 4, 0, -1, -4]
[2]
String
```
!!! note "Note"
before version 21.11 the order of arguments was wrong, i.e. JSON_QUERY(path, json)
## JSON_VALUE(json, path) {#json-value}
Parses a JSON and extract a value as JSON scalar.
If the value does not exist, an empty string will be returned.
Example:
``` sql
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'));
```
Result:
``` text
"world"
0
2
String
```
!!! note "Note"
before version 21.11 the order of arguments was wrong, i.e. JSON_VALUE(path, json)
## toJSONString {#tojsonstring}
Serializes a value to its JSON representation. Various data types and nested structures are supported.
Expand Down
70 changes: 70 additions & 0 deletions docs/ru/sql-reference/functions/json-functions.md
Expand Up @@ -307,6 +307,76 @@ SELECT JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello"
└───────────────────────────────────────────────────────────────────────────────────────────────────────┘
```
## JSON_EXISTS(json, path) {#json-exists}
Если значение существует в документе JSON, то возвращается 1.
Если значение не существует, то возвращается 0.
Пример:
``` sql
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]');
```
!!! note "Примечание"
до версии 21.11 порядок аргументов функции был обратный, т.е. JSON_EXISTS(path, json)
## JSON_QUERY(json, path) {#json-query}
Парсит JSON и извлекает значение как JSON массив или JSON объект.
Если значение не существует, то возвращается пустая строка.
Пример:
``` sql
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'));
```
Результат:
``` text
["world"]
[0, 1, 4, 0, -1, -4]
[2]
String
```
!!! note "Примечание"
до версии 21.11 порядок аргументов функции был обратный, т.е. JSON_QUERY(path, json)
## JSON_VALUE(json, path) {#json-value}
Парсит JSON и извлекает значение как JSON скаляр.
Если значение не существует, то возвращается пустая строка.
Пример:
``` sql
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'));
```
Результат:
``` text
"world"
0
2
String
```
!!! note "Примечание"
до версии 21.11 порядок аргументов функции был обратный, т.е. JSON_VALUE(path, json)
## toJSONString {#tojsonstring}
Expand Down

0 comments on commit cd63b24

Please sign in to comment.