You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Redshift SQL提取嵌套JSON中的指定字段值?

Redshift SQL提取嵌套JSON字符串字段的正确方法

问题核心在于ruleOutput是JSON格式的字符串,而非原生JSON对象。直接嵌套调用json_extract_path_text时,因为传入的是字符串而非可解析的JSON对象,导致无法读取内部字段,最终返回null。

解决方案

分两步完成解析:

  1. 从外层JSON中提取ruleOutput的字符串内容
  2. 用json_parse()将该字符串转换为JSON对象,再提取目标字段

示例SQL

假设output列本身是Redshift JSON类型:

SELECT
  json_extract_path_text(
    json_parse(json_extract_path_text(output, 'executionDetail', 'ruleOutput')),
    'sab_data', 'sab_it_name'
  ) AS sab_it_name,
  json_extract_path_text(
    json_parse(json_extract_path_text(output, 'executionDetail', 'ruleOutput')),
    'sab_data', 'sab_pro_t'
  ) AS sab_pro_t
FROM your_table_name;

如果output列存储的是JSON格式的普通字符串(非Redshift JSON类型),需先将外层字符串转为JSON对象:

SELECT
  json_extract_path_text(
    json_parse(json_extract_path_text(json_parse(output), 'executionDetail', 'ruleOutput')),
    'sab_data', 'sab_it_name'
  ) AS sab_it_name,
  json_extract_path_text(
    json_parse(json_extract_path_text(json_parse(output), 'executionDetail', 'ruleOutput')),
    'sab_data', 'sab_pro_t'
  ) AS sab_pro_t
FROM your_table_name;

内容的提问来源于stack exchange,提问作者Raja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 15:57:33