我的表是这样的:
WITH my_table (event_date, coordinates) AS (
values
('2021-10-01','{"x":"1.0","y":"0.049"}'),
('2021-10-01','{"x":"0.0","y":"0.865"}'),
('2021-10-02','{"y":"0.5","x":"0.5"}'),
('2021-10-02','{"y":"0.469","x":"0.175"}'),
('2021-10-02','{"x":"0.954","y":"0.021"}')
)
SELECT *
FROM my_table
event_date | 坐标 | 2021-10-01 | {"x"1.0","y":"0.049"} |
---|---|
2021-10-01 | {"x"0.0","y":"0.865"} |
2021-10-02 | {"y"0.5","x":"0.5"} |
2021-10-02 | {"y"0.469","x":"0.175"} |
2021-10-02 | {"x"0.954","y":"0.021"} |
尝试解析json数据并单独提取每个点
参考这个:Presto中包含'键的JSON_EXTRACT问题& # 39;字符
在你的例子中,查询应该是:select event_date,json_extract_scalar(coordinates,'$.attributes["x"]') as x, json_extract_scalar(coordinates,'$.attributes["y"]') as y;
更新对不起,我忘记删除"属性"从查询,因为它是从另一个答案:)
select event_date,json_extract_scalar(coordinates,'$.x') as x, json_extract_scalar(coordinates,'$.y') as y;
使用json_extract_scalar
与相应的json路径:
SELECT event_date,
json_extract_scalar(coordinates,'$.x') as x,
json_extract_scalar(coordinates,'$.y') as y
FROM my_table;
输出:
<表类>event_date x y tbody><<tr>2021-10-01 1.0 0.049 2021-10-01 0.0 0.865 2021-10-02 0.5 0.5 2021-10-02 0.175 0.469 2021-10-02 0.954 0.021 表类>