1

I have a jsonb field type in a table, it contains the JArray:

[
  {
    "code": "F01",
    "name": "Apple"
  },
  {
    "code": "F02",
    "name": "Orange"
  },
  {
    "code": "F03",
    "name": "Banana"
  }
]

I try to query based on the code and expecting the name like below:

select a.myarray name from fruits a where a.myarray ->> 'code' = 'F02'

but it returns empty

What I missed?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Don2
  • 313
  • 3
  • 12

1 Answers1

1
select j ->> 'name' 
from jsonb_array_elements(
'[
  {"code": "F01","name": "Apple"},
  {"code": "F02","name": "Orange"},
  {"code": "F03","name": "Banana"}
]'::jsonb) j
where j ->> 'code' = 'F02';

Replace the literal JSON text with the actual value.

Stefanov.sm
  • 11,215
  • 2
  • 21
  • 21