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

如何在Amazon Athena中解析嵌套JSON的RecLoc与FlightNumber字段

在Amazon Athena中提取嵌套JSON字段的方法

你的问题核心是attributes.Message里的requestBody是转义后的JSON字符串,而非直接的JSON对象,所以无法直接访问其内部嵌套字段。需要先将这个字符串解析为JSON结构,再提取目标字段。

方法1:使用json_parse + json_extract_scalar

这是最直观的写法,适合明确提取单个字段的场景:

SELECT
  -- 提取RecLoc字段
  json_extract_scalar(json_parse(attributes.Message), '$.requestBody.RecLoc') AS rec_loc,
  -- 提取FlightInfo嵌套对象中的FlightNumber字段
  json_extract_scalar(json_parse(attributes.Message), '$.requestBody.FlightInfo.FlightNumber') AS flight_number
FROM your_table_name;

方法2:使用简化的->操作符

Athena支持用->简化JSON路径访问,写法更简洁:

SELECT
  json_parse(attributes.Message)->'$.requestBody.RecLoc' AS rec_loc,
  json_parse(attributes.Message)->'$.requestBody.FlightInfo.FlightNumber' AS flight_number
FROM your_table_name;

容错处理

如果日志中存在格式无效的JSON,可以用try_json_parse替代json_parse,解析失败时会返回NULL,避免整个查询报错:

SELECT
  json_extract_scalar(try_json_parse(attributes.Message), '$.requestBody.RecLoc') AS rec_loc,
  json_extract_scalar(try_json_parse(attributes.Message), '$.requestBody.FlightInfo.FlightNumber') AS flight_number
FROM your_table_name;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 21:10:25