跳转到主内容
跳转到主内容

distinctJSONPathsAndTypes

distinctJSONPathsAndTypes

引入版本:v24.9

计算存储在 JSON 列中的不同路径及其对应类型的列表。

注意

如果 JSON 声明中包含显式指定类型的路径,即使输入数据中没有这些路径的值,这些路径也将始终出现在 distinctJSONPaths/distinctJSONPathsAndTypes 函数的结果中。

语法

distinctJSONPathsAndTypes(json)

参数

  • json — JSON 列。JSON

返回值

返回排序后的路径及其类型映射。Map(String, Array(String))

示例

混合类型的基本用法

DROP TABLE IF EXISTS test_json;
CREATE TABLE test_json(json JSON) ENGINE = Memory;
INSERT INTO test_json VALUES ('{"a" : 42, "b" : "Hello"}'), ('{"b" : [1, 2, 3], "c" : {"d" : {"e" : "2020-01-01"}}}'), ('{"a" : 43, "c" : {"d" : {"f" : [{"g" : 42}]}}}');

SELECT distinctJSONPathsAndTypes(json) FROM test_json;
┌─distinctJSONPathsAndTypes(json)───────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ {'a':['Int64'],'b':['Array(Nullable(Int64))','String'],'c.d.e':['Date'],'c.d.f':['Array(JSON(max_dynamic_types=16, max_dynamic_paths=256))']} │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

使用已声明的 JSON 路径

DROP TABLE IF EXISTS test_json;
CREATE TABLE test_json(json JSON(a UInt32)) ENGINE = Memory;
INSERT INTO test_json VALUES ('{"b" : "Hello"}'), ('{"b" : "World", "c" : [1, 2, 3]}');

SELECT distinctJSONPathsAndTypes(json) FROM test_json;
┌─distinctJSONPathsAndTypes(json)────────────────────────────────┐
│ {'a':['UInt32'],'b':['String'],'c':['Array(Nullable(Int64))']} │
└────────────────────────────────────────────────────────────────┘