在 AWS Athena 中将字符串列转换为 JSON

问题描述 投票:0回答:1

我目前在 athena 外部表中有一个包含一个列名称

event
(字符串)的表,我只想将该值作为 JSON 获取。我正在使用以下查询,但它将其转换为字符串:

select CAST(event AS JSON) AS json_event from table); 

输出附加

\
每个双引号
"

表中值:

{"att1": "a", "att2" : "a2"}

CAST() 返回后的上述查询:

"{\"att1\": \"a\", \"att2\" : \"a2\"}

此外,当我运行查询时:

select json_event.attribute
from
(select CAST(event AS JSON) AS json_event from table);

它抛出异常:

TYPE_MISMATCH: line 1:8: Expression json_event is not of type ROW

我做错了什么吗?在此先感谢您的帮助。

sql amazon-web-services amazon-athena presto trino
1个回答
0
投票

使用

json_parse
从输入的JSON文本中获取反序列化的JSON值:

select json_parse('{"att1": "a", "att2" : "a2"}');

输出:

          _col0
--------------------------
 {"att1":"a","att2":"a2"}
© www.soinside.com 2019 - 2024. All rights reserved.