0

I have the following rows in the table src where one of the attributes is an array of JSONs.

Attribute:

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

How does one select the Brand?

Expected output:

Brand
-----
myBrandValue
myBrandOtherValue

(2 rows affected)

I was thinking of using json_to_recordset(json) function, but the attribute can have N different values (jsons).

1
  • 1
    Is this an array of JSON json[] or is this a JSON array? Commented Feb 22, 2021 at 16:52

1 Answer 1

1

demo:db<>fiddle

SELECT
    elems.value ->> 'value'              -- 3
FROM mytable,
    jsonb_array_elements(mydata) elems   -- 1 
WHERE elems.value ->> 'key' = 'Brand'    -- 2
  1. Extract all array elements into one row per JSON element
  2. Filter the JSON elements by key = Brand
  3. Return value
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.