为什么 json_extract 有效但 json_extract_scalar 无效?
Why json_extract works but json_extract_scalar does not?
我有一个数据集,其中包含 json 中的一列,其中有一个属性给我一个列表,我想取消嵌套以加入一些不同的数据。
我考虑过 json_extract_scalar
json_data,然后我可以 split
它,最后 unnest
与其他操作,但是我遇到了问题。
在我的例子中,当我 运行 时 json_extract
它工作正常但我无法转换为 varchar。另一方面,如果我使用 json_extract_scalar
它 returns 一个空值。
我认为问题应该出在引号上,但我不确定如何处理它 - 即使这是正确的问题。
让我给你一个数据样本:
{"my_test_list":["756596263-0","743349523-371296","756112380-0","755061590-0"]}
你们能给我一些建议吗?
我在 Presto 中查询 SQL。
您在键 my_test_list
下存储的是 JSON 数组,而不是标量值 - 这就是为什么 json_extract_scalar()
returns null
.
目前还不清楚您想如何使用这些数据。一个典型的解决方案是 cast
到 array
,然后您可以根据需要使用它,例如取消嵌套。基本语法为:
cast(json_extract(mycol, '$.my_test_list') as array(varchar))
然后您可以在横向连接中使用它,例如:
select t.mycol, x.myval
from mytable t
cross join unnest(
cast(json_extract(mycol, '$.my_test_list') as array(varchar))
) as x(myval)
我有一个数据集,其中包含 json 中的一列,其中有一个属性给我一个列表,我想取消嵌套以加入一些不同的数据。
我考虑过 json_extract_scalar
json_data,然后我可以 split
它,最后 unnest
与其他操作,但是我遇到了问题。
在我的例子中,当我 运行 时 json_extract
它工作正常但我无法转换为 varchar。另一方面,如果我使用 json_extract_scalar
它 returns 一个空值。
我认为问题应该出在引号上,但我不确定如何处理它 - 即使这是正确的问题。
让我给你一个数据样本:
{"my_test_list":["756596263-0","743349523-371296","756112380-0","755061590-0"]}
你们能给我一些建议吗?
我在 Presto 中查询 SQL。
您在键 my_test_list
下存储的是 JSON 数组,而不是标量值 - 这就是为什么 json_extract_scalar()
returns null
.
目前还不清楚您想如何使用这些数据。一个典型的解决方案是 cast
到 array
,然后您可以根据需要使用它,例如取消嵌套。基本语法为:
cast(json_extract(mycol, '$.my_test_list') as array(varchar))
然后您可以在横向连接中使用它,例如:
select t.mycol, x.myval
from mytable t
cross join unnest(
cast(json_extract(mycol, '$.my_test_list') as array(varchar))
) as x(myval)