我的 JSON 数据是:
{
"content":"{\"type\":3,\"from\":\"home\"}",
"id":"239",
"idtype":"0",
"timestamp":"1547957367281",
"type":"0"
}
我想将其放入以下格式的表 json_data 中:
+-------------+
| from |
+-------------+
| home |
+-------------+
我如何使用此处的爆炸函数来获得所需的输出?
最佳答案
您可以使用 regexp_replace 删除 \
,同时 {
之前和 之后的
"
也应该被删除。使用 get_json_object
或 json_tuple 提取属性。在您的数据示例上测试它:
select get_json_object(json,'$.content.from') as `from`
from
(
select
regexp_replace(
regexp_replace(
regexp_replace(
'{"content":"{\"type\":3,\"from\":\"home\"}",
"id":"239",
"idtype":"0",
"timestamp":"1547957367281",
"type":"0"
}' --original data
,'\\\"','\"') --replace \" with "
,'\\"\\{','{') --remove " before {
,'\\}\\"','\\}') --remove " after } --these last two can be combined
as json
)s
;
输出:
OK
from
home
Time taken: 0.329 seconds, Fetched: 1 row(s)
最好逐一检查这些 regexp_replaces 以确保它按预期工作。希望你明白了
关于json - 配置单元:解析 JSON 包含\",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54289524/