0
votes

I have a jsonb field with an array like this one below:

[  
   {  
      "type":"discount",
      "title":"Discount 10%"
   },
   {        
      "file":"zx5rP8EoacyfhqGndcSOnP8VYtkr9Ya8Nvf7oYL98YDsM1CLMYIurYvfVUU4AGkzBsovwssT0bq.pdf",
      "type":"menu",
      "title":"Some menu title etc"
   }
]

I want to get the file attribute in case there is a type=menu in the array.

What I managed to do is to know if there is one, but how can I eventually extract the file value?

case when offers @> '[{"type":"menu"}]' then true else false end

I don't want to do something like this below because the array may not contain a discount type.

offers->1->'file'
2
try json_array_elements for this?.. - Vao Tsun
@VaoTsun no luck there - mallix

2 Answers

2
votes

Use jsob_array_elements() and ->> operator (see JSON Functions and Operators.)

with a_table(json_col) as (
values (
'[  
   {  
      "type":"discount",
      "title":"Discount 10%"
   },
   {        
      "file":"zx5rP8EoacyfhqGndcSOnP8VYtkr9Ya8Nvf7oYL98YDsM1CLMYIurYvfVUU4AGkzBsovwssT0bq.pdf",
      "type":"menu",
      "title":"Some menu title etc"
   }
]'::jsonb)
)

select value->>'file' as filename
from a_table,
lateral jsonb_array_elements(json_col)
where value->>'type' = 'menu'

                                    filename                                     
---------------------------------------------------------------------------------
 zx5rP8EoacyfhqGndcSOnP8VYtkr9Ya8Nvf7oYL98YDsM1CLMYIurYvfVUU4AGkzBsovwssT0bq.pdf
(1 row)
1
votes

Eg:

t=# with a as (with v as (select '[
   {
      "type":"discount",
      "title":"Discount 10%"
   },
   {
      "file":"zx5rP8EoacyfhqGndcSOnP8VYtkr9Ya8Nvf7oYL98YDsM1CLMYIurYvfVUU4AGkzBsovwssT0bq.pdf",
      "type":"menu",
      "title":"Some menu title etc"
   }
]'::jsonb j)
select jsonb_array_elements(j) r from v) select r->>'file' from a where r->>'type' = 'menu';
                                    ?column?
---------------------------------------------------------------------------------
 zx5rP8EoacyfhqGndcSOnP8VYtkr9Ya8Nvf7oYL98YDsM1CLMYIurYvfVUU4AGkzBsovwssT0bq.pdf
(1 row)