【发布时间】:2021-05-17 21:55:49
【问题描述】:
背景
我有一张表,其中一列包含如下所示的数据:
[{"category": "Sports & Outdoors", "rank": 218, "link": "www.foop.com"},{"category": "Bike Saddle Covers", "rank": 1, "link" : "www.foo2.com"}]
据我了解,上面是一个json数组。我尝试了select json_array_length(col_1) from mytable 并且能够返回 2 的长度,所以我知道它是一个 json 数组。
问题
我希望从数组中的每个 json 中提取键 category 的值。
我不确定如何进行。我知道如果它是一个简单的 json,我可以做类似 select col_name -> 'category' from table 的事情。
我尝试了什么
`select array_to_json(col_1) from mytable`
错误:function array_to_json(character varying) does not exist
我也试过select array_to_json(col_1::varchar) from mytable
我很感激这里的任何帮助,因为我对 sql 非常陌生,并且只完成了基本的查询。
【问题讨论】:
-
@Serge 谢谢。我厌倦了您的建议,但出现以下错误:语法错误:数据库:prod-redshift (Amazon Redshift) at or near "select"
标签: json amazon-redshift