如果该列具有如下值
[{"name": "my_name_1"}] [{"name": "my_name_2"}] [{"name": "my_name_3"}] [{"name": "my_name_4"}]
我如何只my_name_1
使用 json_extract?
我尝试了以下但没有奏效JSON_EXTRACT_SCALAR(column_name,"$.name")
如果该列具有如下值
[{"name": "my_name_1"}] [{"name": "my_name_2"}] [{"name": "my_name_3"}] [{"name": "my_name_4"}]
我如何只my_name_1
使用 json_extract?
我尝试了以下但没有奏效JSON_EXTRACT_SCALAR(column_name,"$.name")
试试这个:
with mytable as (
select '[{"name": "my_name_1"}] [{"name": "my_name_2"}] [{"name": "my_name_3"}] [{"name": "my_name_4"}]' as mycolumn
)
select JSON_EXTRACT_SCALAR(split(trim(mycolumn, ']['), '] [')[OFFSET(0)],"$.name")
from mytable
你可以试试OFFSET
,因为你在列中有一个数组
JSON_EXTRACT_SCALAR(column_name[OFFSET(0)],"$.name")
你的数据看起来像这样
WITH t0 AS (
SELECT ARRAY['{"name": "my_name_1"}'] column_name
UNION ALL
SELECT ARRAY['{"name": "my_name_2"}']
UNION ALL
SELECT ARRAY['{"name": "my_name_3"}']
UNION ALL
SELECT ARRAY['{"name": "my_name_4"}']
)
SELECT JSON_EXTRACT_SCALAR(column_name[OFFSET(0)],"$.name") name FROM t0