如何在 AWS Athena 中解析 Json?

问题描述

我想在 Athena 中解析 JSON 列,但我在某一列中遇到了问题。该列包含一个转义字符。不知道有没有这个问题。我想联系消息。您可以在返回下看到以下示例数据和消息。谢谢

Sample Code 
CAST(json_extract_scalar(callparameters,'$return.Message') AS VARCHAR) AS Message

这是 JSON。

{"inputs":{"usagespecid":"null","playsessionid":"null","satellitetypecd":"\"ERU\"","channelcontentid":"231"},"return":"{\"Message\":\"Do not person you have called \"}"}    {"date":"2021-07-26","epoch":"1627"}    {"userpartyid":"23","userloginkey":"23","usercountrycode":"br","sessionkey":"23","client":{"name":"SMR","id":"1"},"usermainsatellitetype":"DS","userserviceaccountids":["23"],"usergeolocationid":"53","userpartyroleid":"76"}

解决方法

如果只取包含 return 的对象,则可以使用 json_parsereturn 解析为 json,因为它包含编码字符串而不是 json 对象:

WITH dataset AS (
    SELECT * FROM (VALUES          
       (JSON '{
   "inputs":{
      "usagespecid":"null","playsessionid":"null","satellitetypecd":"\"ERU\"","channelcontentid":"231"
   },"return":"{\"Message\":\"Do not person you have called \"}"
}')
 ) AS t (json_string))


SELECT json_extract_scalar(json_parse(json_extract_scalar(json_string,'$.return')),'$.Message')
FROM dataset

输出:

_col0
不要找你打电话的人