Lakehouse JSON 数据解析指南

概述

半结构化数据(如 JSON)在现代数据平台中越来越常见。云器 Lakehouse 提供原生的

JSON
JSON
数据类型和丰富的 JSON 函数,支持高效的 JSON 解析、提取和转换。本指南按业务场景分类,帮助您快速掌握 JSON 数据处理方法。

涉及的 SQL 命令

命令/函数用途适用场景
PARSE_JSON(str)
PARSE_JSON(str)
字符串转 JSON将 JSON 字符串解析为 JSON 类型
json_extract_string(json, path)
json_extract_string(json, path)
提取字符串值提取 JSON 字段为 STRING
json_extract_int(json, path)
json_extract_int(json, path)
提取整数值提取 JSON 字段为 INT
json_extract(json, path)
json_extract(json, path)
提取 JSON 对象/数组提取嵌套结构,返回 JSON 类型
LATERAL VIEW EXPLODE(...)
LATERAL VIEW EXPLODE(...)
展开 JSON 数组将数组元素转为多行
TO_JSON(struct)
TO_JSON(struct)
结构体转 JSON将查询结果转为 JSON 字符串

前置准备

以下示例使用模拟的用户行为日志表

user_events
user_events

-- 创建测试表 CREATE TABLE IF NOT EXISTS user_events ( event_id INT, user_id INT, event_type STRING, payload STRING ); -- 插入测试数据(JSON 字符串) INSERT INTO user_events VALUES (1, 101, 'page_view', '{"page": "home", "duration": 30, "tags": ["tech", "news"]}'), (2, 102, 'purchase', '{"item": "laptop", "price": 8000, "details": {"color": "silver", "warranty": 2}}'), (3, 101, 'click', '{"button": "buy_now", "metadata": {"source": "app", "version": "2.1"}}');


JSON 字符串解析

将存储为

STRING
STRING
的 JSON 数据解析为 Lakehouse 原生的
JSON
JSON
类型,以便使用 JSON 函数。

-- 解析 JSON 字符串 SELECT event_id, user_id, PARSE_JSON(payload) as json_payload FROM user_events WHERE event_id = 1;

结果说明

event_iduser_idjson_payload
1101{"page":"home","duration":30,"tags":["tech","news"]}

提取标量值

使用

json_extract_*
json_extract_*
函数提取 JSON 中的具体字段值,并转换为对应类型。

-- 提取页面名称和停留时长 SELECT event_id, json_extract_string(PARSE_JSON(payload), '$.page') as page_name, json_extract_int(PARSE_JSON(payload), '$.duration') as duration_sec FROM user_events WHERE event_type = 'page_view';

结果说明

event_idpage_nameduration_sec
1home30

路径语法

  • $.field
    $.field
    :提取根级别字段
  • $.parent.child
    $.parent.child
    :提取嵌套字段
  • $.array[0]
    $.array[0]
    :提取数组指定索引元素

提取嵌套对象

当需要提取整个子对象或数组时,使用

json_extract
json_extract
,返回
JSON
JSON
类型。

-- 提取购买详情(嵌套对象) SELECT event_id, json_extract(PARSE_JSON(payload), '$.details') as item_details FROM user_events WHERE event_type = 'purchase';

结果说明

event_iditem_details
2{"color":"silver","warranty":2}

提取后可继续使用

json_extract_string
json_extract_string
进一步解析:

-- 提取嵌套字段 SELECT event_id, json_extract_string(json_extract(PARSE_JSON(payload), '$.details'), '$.color') as color FROM user_events WHERE event_type = 'purchase';

结果说明

event_idcolor
2silver

JSON 展开为行

将 JSON 数组展开为多行,常用于标签、属性列表等场景。Lakehouse 中推荐使用

EXPLODE
EXPLODE
配合类型转换。

-- 展开标签数组 SELECT event_id, user_id, tag FROM user_events, EXPLODE(CAST(json_extract(PARSE_JSON(payload), '$.tags') AS ARRAY<STRING>)) AS t(tag) WHERE event_type = 'page_view';

结果说明

event_iduser_idtag
1101tech
1101news

构建 JSON 对象

将查询结果或结构体转换为 JSON 字符串,常用于 API 响应或数据导出。

-- 将行数据转为 JSON SELECT event_id, TO_JSON(NAMED_STRUCT( 'user_id', user_id, 'event_type', event_type, 'timestamp', CURRENT_TIMESTAMP() )) as event_json FROM user_events WHERE event_id = 1;

结果说明

event_idevent_json
1{"user_id":101,"event_type":"page_view","timestamp":"2024-06-01T10:00:00Z"}

清理测试数据

完成 JSON 解析验证后,建议清理测试表:

-- 删除测试表 DROP TABLE IF EXISTS user_events;


注意事项

  1. PARSE_JSON 性能:如果列已经是
    JSON
    JSON
    类型,无需调用
    PARSE_JSON
    PARSE_JSON
  2. 路径不存在
    json_extract_*
    json_extract_*
    在路径不存在时返回
    NULL
    NULL
    ,不会报错。
  3. 类型匹配:使用正确的提取函数(如
    json_extract_int
    json_extract_int
    提取数字),否则可能需要额外
    CAST
    CAST
  4. FROM_JSON 大小写陷阱
    FROM_JSON
    FROM_JSON
    解析 schema 时会将字段名强制转为小写。如需保留大写,使用
    PARSE_JSON
    PARSE_JSON
  5. 数组展开:使用
    EXPLODE(CAST(json_extract(...) AS ARRAY<TYPE>))
    EXPLODE(CAST(json_extract(...) AS ARRAY<TYPE>))
    将 JSON 数组展开为多行。
  6. 数组索引:JSON 数组索引从 0 开始,如
    $.tags[0]
    $.tags[0]
    获取第一个元素。

相关文档

联系我们
预约咨询
微信咨询
电话咨询