Postgres parse array/json



我在表src中有以下行,其中一个属性是json数组。

属性:

[{"key": "Tag", "value": "myTagValue"}, {"key": "Brand", "value": "myBrandValue"}]
[{"key": "Tag", "value": "myTagValue"}, {"key": "Brand", "value": "myBrandOtherValue"}, {"key": "Test", "value": "123"}]

如何选择Brand?

预期输出:

Brand
-----
myBrandValue
myBrandOtherValue
(2 rows affected)

我想使用json_to_recordset(json)函数,但属性可以有N不同的值(jsons)。

demo:db<>fiddle

SELECT
elems.value ->> 'value'              -- 3
FROM mytable,
jsonb_array_elements(mydata) elems   -- 1 
WHERE elems.value ->> 'key' = 'Brand'    -- 2
  1. 每个JSON元素提取所有数组元素到一行
  2. 通过key = Brand筛选JSON元素
  3. 返回value

最新更新