根据 Amazon Redshift 中的 json 数组中的特定键提取值
Extract value based on specific key from array of jsons in Amazon Redshift
我在 Redshift 中有一个 table 说 T1,列名是 col1 数据类型是 varchar。
然而数据存储在 col1 是 json data.
col1 值:
[{"id":1,"value":null},{"id":2,"value":null},{"id":3,"value":"https://www.google.co.in"},{"id":4,"value":"India"}]
现在我想根据键提取数据,即如果我在 where 子句中提供 id 的值,它应该获取与该 id 对应的值
我试过以下查询:
select json_extract_array_element_text(col1, 0) as json_value,
json_extract_path_text(json_value, 'value') as value
from T1
我无法在上述查询中找到基于 id 进行过滤的方法。
如果有人能帮助我,我将不胜感激。
您需要将您的数据与 json 数组交叉连接,其中的整数列表足够长以提取所有数组元素。当您将行“分解”出来时,您可以提取每个行的“id”和“value”,然后应用所需的 WHERE 子句。
在 SO 中有很多这样做的例子,但如果您需要进一步的帮助,请在评论中联系我们。
=========================================
演示 SQL 将 json 数组和元素分解为数据库行和列的步骤。我已将每一步都作为 CTE,以期提高清晰度。还应注意,对大量本机 json 数据执行查询可能会非常慢,建议将经常查询的数据转换为不同的列。此外,所编写的代码只会扩展 json 数组的 10 个元素 - 如果需要的元素比这更多,那么数字 CTE 将需要更多的值。
with recursive numbers(n) as -- this creates the numbers 0 - 9
( select 0 as n
union all
select n + 1
from numbers n
where n.n < 9
),
inputtext as -- put your input string into a CTE; you may have a table for this
( select '[{"id":1,"value":null},{"id":2,"value":null},{"id":3,"value":"https://www.google.co.in"},{"id":4,"value":"India"}]'::text as col1
),
exploded as -- cross join the input data with the numbers to extract all the array elements
( select json_extract_array_element_text(col1, n) as json_value
from inputtext cross join numbers
),
smashed as -- extract the json elements id and value
( select json_extract_path_text(json_value, 'id') as id, json_extract_path_text(json_value, 'value') as value
from exploded
where json_value is not null
)
select id, value -- select the id & value of interest
from smashed
where id = 3
;
我在 Redshift 中有一个 table 说 T1,列名是 col1 数据类型是 varchar。 然而数据存储在 col1 是 json data.
col1 值:
[{"id":1,"value":null},{"id":2,"value":null},{"id":3,"value":"https://www.google.co.in"},{"id":4,"value":"India"}]
现在我想根据键提取数据,即如果我在 where 子句中提供 id 的值,它应该获取与该 id 对应的值
我试过以下查询:
select json_extract_array_element_text(col1, 0) as json_value,
json_extract_path_text(json_value, 'value') as value
from T1
我无法在上述查询中找到基于 id 进行过滤的方法。
如果有人能帮助我,我将不胜感激。
您需要将您的数据与 json 数组交叉连接,其中的整数列表足够长以提取所有数组元素。当您将行“分解”出来时,您可以提取每个行的“id”和“value”,然后应用所需的 WHERE 子句。
在 SO 中有很多这样做的例子,但如果您需要进一步的帮助,请在评论中联系我们。
=========================================
演示 SQL 将 json 数组和元素分解为数据库行和列的步骤。我已将每一步都作为 CTE,以期提高清晰度。还应注意,对大量本机 json 数据执行查询可能会非常慢,建议将经常查询的数据转换为不同的列。此外,所编写的代码只会扩展 json 数组的 10 个元素 - 如果需要的元素比这更多,那么数字 CTE 将需要更多的值。
with recursive numbers(n) as -- this creates the numbers 0 - 9
( select 0 as n
union all
select n + 1
from numbers n
where n.n < 9
),
inputtext as -- put your input string into a CTE; you may have a table for this
( select '[{"id":1,"value":null},{"id":2,"value":null},{"id":3,"value":"https://www.google.co.in"},{"id":4,"value":"India"}]'::text as col1
),
exploded as -- cross join the input data with the numbers to extract all the array elements
( select json_extract_array_element_text(col1, n) as json_value
from inputtext cross join numbers
),
smashed as -- extract the json elements id and value
( select json_extract_path_text(json_value, 'id') as id, json_extract_path_text(json_value, 'value') as value
from exploded
where json_value is not null
)
select id, value -- select the id & value of interest
from smashed
where id = 3
;