I have JSON stored in a jsonb column:
[
{
"ORDER_TYPE": "foo",
"PAYMENT_TYPE": "VISA",
}
]
I can query fine but is it possible to select specific parts (PAYMENT_TYPE) of the json from the resultset?
SELECT PAYMENT_TYPE
FROM tools.orders
WHERE responsejsonb::jsonb @> '[{"ORDER_TYPE":"foo"}]';
Desired output "PAYMENT_TYPE": "VISA" or "VISA"
2
Answers
Flatten the JSONB array first.
Edit
If however the
responsejsonb
array has only one element (or only the first one matters) then it’s simpler, the expression that you need isresponsejsonb::jsonb->0->>'PAYMENT_TYPE'
.You can use a JSON path expression:
If you want all payment types, use
jsonb_path_query_array()
instead.Online example
If
responsejsonb
is really defined with thejsonb
type, the cast to jsonb is useless.