Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get element from array of JSON postgres

I have a column x with data type jsonb

and the value looks like:

[  
    [{"string":"whateverstring1"}],
    [{"string":"whateverstring2"}]
]

How to return each element of the array?

Something like this: "whateverstring1","whateverstring2"

like image 533
Mo Halem Avatar asked Dec 18 '22 20:12

Mo Halem


1 Answers

demo:db<>fiddle

SELECT jsonb_array_elements(jsonb) -> 0 -> 'string' 
FROM (
    SELECT '[[{"string":"whateverstring1"}],[{"string":"whateverstring2"}]]'::jsonb
) s
  1. jsonb_array_elements extract each element into one row
  2. -> 0 gives the first element of the nested arrays which is {"string":"whateverstring1"}
  3. -> 'string' gives the value of the elements
like image 144
S-Man Avatar answered Jan 01 '23 17:01

S-Man