diff --git a/docs/en/sql-reference/functions/json-functions.md b/docs/en/sql-reference/functions/json-functions.md index eed6ef4b7bfe0662efbc3edf0f341c6743a65604..cf3e352eba90b9e7b040f8ef6860ec4567ee67a5 100644 --- a/docs/en/sql-reference/functions/json-functions.md +++ b/docs/en/sql-reference/functions/json-functions.md @@ -1,6 +1,6 @@ --- toc_priority: 56 -toc_title: Working with JSON. +toc_title: Working with JSON --- # Functions for Working with JSON {#functions-for-working-with-json} @@ -226,14 +226,71 @@ Example: SELECT JSONExtractArrayRaw('{"a": "hello", "b": [-100, 200.0, "hello"]}', 'b') = ['-100', '200.0', '"hello"']' ``` -[Original article](https://clickhouse.tech/docs/en/query_language/functions/json_functions/) +## JSONExtractKeysAndValuesRaw {#json-extract-keys-and-values-raw} -## JSONExtractKeysAndValuesRaw(json\[, indices\_or\_keys…\]) {#jsonextractkeysandvaluesrawjson-indices-or-keys} +Extracts raw data from a JSON object. -Parses key-value pairs from a JSON and returns an array of such pairs, each value represented as unparsed string. +**Syntax** -Example: +``` sql +JSONExtractKeysAndValuesRaw(json[, p, a, t, h]) +``` + +**Parameters** + +- `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. + +**Returned values** + +- Array with `('key', 'value')` tuples. Both tuple members are strings. +- Empty array if the requested object does not exist, or input JSON is invalid. + +Type: [Array](../data-types/array.md)([Tuple](../data-types/tuple.md)([String](../data-types/string.md), [String](../data-types/string.md)). + +**Examples** + +Query: ``` sql -SELECT JSONExtractKeysAndValuesRaw('{"a": "hello", "b": [-100, 200.0, 300]}') = [('a','"hello"'),('b','[-100,200,300]')] +SELECT JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}') +``` + +Result: + +``` text +┌─JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}')─┐ +│ [('a','[-100,200]'),('b','{"c":{"d":"hello","f":"world"}}')] │ +└──────────────────────────────────────────────────────────────────────────────────────────────┘ ``` + +Query: + +``` sql +SELECT JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}', 'b') +``` + +Result: + +``` text +┌─JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}', 'b')─┐ +│ [('c','{"d":"hello","f":"world"}')] │ +└───────────────────────────────────────────────────────────────────────────────────────────────────┘ +``` + +Query: + +``` sql +SELECT JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}', -1, 'c') +``` + +Result: + +``` text +┌─JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}', -1, 'c')─┐ +│ [('d','"hello"'),('f','"world"')] │ +└───────────────────────────────────────────────────────────────────────────────────────────────────────┘ +``` + + +[Original article](https://clickhouse.tech/docs/en/query_language/functions/json_functions/) diff --git a/docs/ru/sql-reference/functions/json-functions.md b/docs/ru/sql-reference/functions/json-functions.md index 5fd93f16a5cde559cbc889a7f964814d4c35e38c..1079702e70906744e25b700b98ad5611f090cd73 100644 --- a/docs/ru/sql-reference/functions/json-functions.md +++ b/docs/ru/sql-reference/functions/json-functions.md @@ -221,4 +221,72 @@ SELECT JSONExtractRaw('{"a": "hello", "b": [-100, 200.0, 300]}', 'b') = '[-100, SELECT JSONExtractArrayRaw('{"a": "hello", "b": [-100, 200.0, "hello"]}', 'b') = ['-100', '200.0', '"hello"']' ``` +## JSONExtractKeysAndValuesRaw {#json-extract-keys-and-values-raw} + +Извлекает необработанные данные из объекта JSON. + +**Синтаксис** + +``` sql +JSONExtractKeysAndValuesRaw(json[, p, a, t, h]) +``` + +**Параметры** + +- `json` — [Строка](../data-types/string.md), содержащая валидный JSON. +- `p, a, t, h` — Индексы или ключи, разделенные запятыми, которые указывают путь к внутреннему полю во вложенном объекте JSON. Каждый аргумент может быть либо [строкой](../data-types/string.md) для получения поля по ключу, либо [целым числом](../data-types/int-uint.md) для получения N-го поля (индексирование начинается с 1, отрицательные числа используются для отсчета с конца). Если параметр не задан, весь JSON парсится как объект верхнего уровня. Необязательный параметр. + +**Возвращаемые значения** + +- Массив с кортежами `('key', 'value')`. Члены кортежа — строки. + +- Пустой массив, если заданный объект не существует или входные данные не валидный JSON. + +Тип: Type: [Array](../data-types/array.md)([Tuple](../data-types/tuple.md)([String](../data-types/string.md), [String](../data-types/string.md)). +. + +**Примеры** + +Запрос: + +``` sql +SELECT JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}') +``` + +Ответ: + +``` text +┌─JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}')─┐ +│ [('a','[-100,200]'),('b','{"c":{"d":"hello","f":"world"}}')] │ +└──────────────────────────────────────────────────────────────────────────────────────────────┘ +``` + +Запрос: + +``` sql +SELECT JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}', 'b') +``` + +Ответ: + +``` text +┌─JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}', 'b')─┐ +│ [('c','{"d":"hello","f":"world"}')] │ +└───────────────────────────────────────────────────────────────────────────────────────────────────┘ +``` + +Запрос: + +``` sql +SELECT JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}', -1, 'c') +``` + +Ответ: + +``` text +┌─JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}', -1, 'c')─┐ +│ [('d','"hello"'),('f','"world"')] │ +└───────────────────────────────────────────────────────────────────────────────────────────────────────┘ +``` + [Оригинальная статья](https://clickhouse.tech/docs/ru/query_language/functions/json_functions/)