ClickHouse/docs/fr/sql-reference/functions/json-functions.md
Ivan Blinkov cd14f9ebcb
SQL reference refactoring (#10857)
* split up select.md

* array-join.md basic refactoring

* distinct.md basic refactoring

* format.md basic refactoring

* from.md basic refactoring

* group-by.md basic refactoring

* having.md basic refactoring

* additional index.md refactoring

* into-outfile.md basic refactoring

* join.md basic refactoring

* limit.md basic refactoring

* limit-by.md basic refactoring

* order-by.md basic refactoring

* prewhere.md basic refactoring

* adjust operators/index.md links

* adjust sample.md links

* adjust more links

* adjust operatots links

* fix some links

* adjust aggregate function article titles

* basic refactor of remaining select clauses

* absolute paths in make_links.sh

* run make_links.sh

* remove old select.md locations

* translate docs/es

* translate docs/fr

* translate docs/fa

* remove old operators.md location

* change operators.md links

* adjust links in docs/es

* adjust links in docs/es

* minor texts adjustments

* wip

* update machine translations to use new links

* fix changelog

* es build fixes

* get rid of some select.md links

* temporary adjust ru links

* temporary adjust more ru links

* improve curly brace handling

* adjust ru as well

* fa build fix

* ru link fixes

* zh link fixes

* temporary disable part of anchor checks
2020-05-15 07:34:54 +03:00

12 KiB
Raw Blame History

machine_translated machine_translated_rev toc_priority toc_title
true 72537a2d52 56 Travailler avec JSON

Fonctions pour travailler avec JSON

Dans Yandex.Metrica, JSON est transmis par les utilisateurs en tant que paramètres de session. Il y a quelques fonctions spéciales pour travailler avec ce JSON. (Bien que dans la plupart des cas, les JSONs soient en outre prétraités et les valeurs résultantes sont placées dans des colonnes séparées dans leur format traité.) Toutes ces fonctions sont basées sur des hypothèses fortes sur ce que le JSON peut être, mais elles essaient de faire le moins possible pour faire le travail.

Les hypothèses suivantes sont apportées:

  1. Le nom du champ (argument de fonction) doit être une constante.
  2. Le nom du champ est en quelque sorte codé canoniquement dans JSON. Exemple: visitParamHas('{"abc":"def"}', 'abc') = 1, mais visitParamHas('{"\\u0061\\u0062\\u0063":"def"}', 'abc') = 0
  3. Les champs sont recherchés à n'importe quel niveau d'imbrication, sans discrimination. S'il y a plusieurs champs correspondants, la première occurrence est utilisé.
  4. Le JSON n'a pas de caractères d'espace en dehors des littéraux de chaîne.

visitParamHas(params, nom)

Vérifie s'il existe un champ avec name nom.

visitParamExtractUInt(params, nom)

Analyse UInt64 à partir de la valeur du champ nommé name. Si c'est un champ de type chaîne, il tente d'analyser un numéro à partir du début de la chaîne. Si le champ n'existe pas, ou s'il existe mais ne contient pas de nombre, il renvoie 0.

visitParamExtractInt(params, name)

Le même que pour Int64.

visitParamExtractFloat(params, nom)

Le même que pour Float64.

visitParamExtractBool(params, nom)

Analyse d'une valeur vrai/faux. Le résultat est UInt8.

visitParamExtractRaw(params, nom)

Retourne la valeur d'un champ, y compris les séparateurs.

Exemple:

visitParamExtractRaw('{"abc":"\\n\\u0000"}', 'abc') = '"\\n\\u0000"'
visitParamExtractRaw('{"abc":{"def":[1,2,3]}}', 'abc') = '{"def":[1,2,3]}'

visitParamExtractString(params, nom)

Analyse la chaîne entre guillemets doubles. La valeur est sans échappement. Si l'échappement échoue, il renvoie une chaîne vide.

Exemple:

visitParamExtractString('{"abc":"\\n\\u0000"}', 'abc') = '\n\0'
visitParamExtractString('{"abc":"\\u263a"}', 'abc') = '☺'
visitParamExtractString('{"abc":"\\u263"}', 'abc') = ''
visitParamExtractString('{"abc":"hello}', 'abc') = ''

Il n'y a actuellement aucun support pour les points de code dans le format \uXXXX\uYYYY qui ne proviennent pas du plan multilingue de base (ils sont convertis en CESU-8 au lieu de UTF-8).

Les fonctions suivantes sont basées sur simdjson conçu pour des exigences D'analyse JSON plus complexes. L'hypothèse 2 mentionnée ci-dessus s'applique toujours.

isValidJSON (json)

Vérifie que la chaîne est un json valide.

Exemple:

SELECT isValidJSON('{"a": "hello", "b": [-100, 200.0, 300]}') = 1
SELECT isValidJSON('not a json') = 0

JSONHas(json[, indices_or_keys]…)

Si la valeur existe dans le document JSON, 1 sera retourné.

Si la valeur n'existe pas, 0 sera retourné.

Exemple:

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 est une liste de zéro ou plusieurs arguments chacun d'entre eux peut être une chaîne ou un entier.

  • String = membre d'objet d'accès par clé.
  • Entier positif = accédez au n-ème membre / clé depuis le début.
  • Entier négatif = accédez au n-ème membre / clé à partir de la fin.

Minimum de l'indice de l'élément est 1. Ainsi, l'élément 0 n'existe pas.

Vous pouvez utiliser des entiers pour accéder à la fois aux tableaux JSON et aux objets JSON.

Ainsi, par exemple:

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'

JSONLength(json[, indices_or_keys]…)

Renvoie la longueur D'un tableau JSON ou d'un objet JSON.

Si la valeur n'existe pas ou a un mauvais type, 0 sera retourné.

Exemple:

SELECT JSONLength('{"a": "hello", "b": [-100, 200.0, 300]}', 'b') = 3
SELECT JSONLength('{"a": "hello", "b": [-100, 200.0, 300]}') = 2

JSONType(json[, indices_or_keys]…)

De retour le type d'une valeur JSON.

Si la valeur n'existe pas, Null sera retourné.

Exemple:

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'

JSONExtractUInt(json[, indices_or_keys]…)

JSONExtractInt(json[, indices_or_keys]…)

JSONExtractFloat(json[, indices_or_keys]…)

JSONExtractBool(json[, indices_or_keys]…)

Analyse un JSON et extrait une valeur. Ces fonctions sont similaires à visitParam fonction.

Si la valeur n'existe pas ou a un mauvais type, 0 sera retourné.

Exemple:

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

JSONExtractString(json[, indices_or_keys]…)

Analyse un JSON et extrait une chaîne. Cette fonction est similaire à visitParamExtractString fonction.

Si la valeur n'existe pas ou a un mauvais type, une chaîne vide est retournée.

La valeur est sans échappement. Si l'échappement échoue, il renvoie une chaîne vide.

Exemple:

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') = ''

JSONExtract(json[, indices_or_keys…], Return_type)

Analyse un JSON et extrait une valeur du type de données clickhouse donné.

C'est une généralisation de la précédente JSONExtract<type> fonction. Cela signifie JSONExtract(..., 'String') retourne exactement le même que JSONExtractString(), JSONExtract(..., 'Float64') retourne exactement le même que JSONExtractFloat().

Exemple:

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)

Analyse les paires clé-valeur à partir D'un JSON où les valeurs sont du type de données clickhouse donné.

Exemple:

SELECT JSONExtractKeysAndValues('{"x": {"a": 5, "b": 7, "c": 11}}', 'x', 'Int8') = [('a',5),('b',7),('c',11)]

JSONExtractRaw(json[, indices_or_keys]…)

Renvoie une partie de JSON en tant que chaîne non analysée.

Si la pièce n'existe pas ou a un mauvais type, une chaîne vide est retournée.

Exemple:

SELECT JSONExtractRaw('{"a": "hello", "b": [-100, 200.0, 300]}', 'b') = '[-100, 200.0, 300]'

JSONExtractArrayRaw(json[, indices_or_keys…])

Retourne un tableau avec des éléments de tableau JSON, chacun représenté comme une chaîne non analysée.

Si la pièce n'existe pas ou n'est pas de tableau, un tableau vide sera retournée.

Exemple:

SELECT JSONExtractArrayRaw('{"a": "hello", "b": [-100, 200.0, "hello"]}', 'b') = ['-100', '200.0', '"hello"']'

JSONExtractKeysAndValuesRaw

Extrait les données brutes d'un objet JSON.

Syntaxe

JSONExtractKeysAndValuesRaw(json[, p, a, t, h])

Paramètre

  • jsonChaîne avec JSON valide.
  • 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 chaîne pour obtenir le champ par la touche ou un entier pour obtenir le N-ème champ (indexé à partir de 1, les entiers négatifs comptent à partir de la fin). S'il n'est pas défini, le JSON entier est analysé en tant qu'objet de niveau supérieur. Paramètre facultatif.

Valeurs renvoyées

  • Tableau avec ('key', 'value') tuple. Les deux membres du tuple sont des chaînes.
  • Tableau vide si l'objet demandé n'existe pas, ou entrée JSON n'est pas valide.

Type: Tableau(Tuple(Chaîne, Chaîne).

Exemple

Requête:

SELECT JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}')

Résultat:

┌─JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}')─┐
│ [('a','[-100,200]'),('b','{"c":{"d":"hello","f":"world"}}')]                                 │
└──────────────────────────────────────────────────────────────────────────────────────────────┘

Requête:

SELECT JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}', 'b')

Résultat:

┌─JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}', 'b')─┐
│ [('c','{"d":"hello","f":"world"}')]                                                               │
└───────────────────────────────────────────────────────────────────────────────────────────────────┘

Requête:

SELECT JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}', -1, 'c')

Résultat:

┌─JSONExtractKeysAndValuesRaw('{"a": [-100, 200.0], "b":{"c": {"d": "hello", "f": "world"}}}', -1, 'c')─┐
│ [('d','"hello"'),('f','"world"')]                                                                     │
└───────────────────────────────────────────────────────────────────────────────────────────────────────┘

Article Original