I have a table with a column containing a JSON array. As an example:
with example as (
select '["a", "b"]'::jsonb as col
union select ('["c", "d", "e"]'::jsonb) as col
)
select col from example
Returns:
col
["a", "b"]
["c", "d", "e"]
I can use jsonb_array_elements to expand out each array into rows:
select jsonb_array_elements(col) from example
Returns:
jsonb_array_elements
"a"
"b"
"c"
"d"
"e"
I want the index of each array element along with the element itself (a bit like Python's enumerate), like so:
jsonb_array_elements array_index
"a" 1
"b" 2
"c" 1
"d" 2
"e" 3
How can I do this?
My application has read-only access, so I cannot create functions.