Manipulating JSONB with jsonb_each_text() to Expand Fields
Use jsonb_each_text() to expand JSONB fields into key-value rows, simplifying data analysis and manipulation in PostgreSQL.
-- Expanding JSONB to view key and value
SELECT key, value
FROM orders o,
LATERAL jsonb_each_text(o.details)
WHERE o.order_id = '12345';
-- Filtering and ordering the order status history
SELECT
o.order_id,
key,
value,
o.created_at
FROM orders o,
LATERAL jsonb_each_text(o.details)
WHERE
o.order_id = '12345' AND
key = 'status'
ORDER BY o.created_at DESC;Last updated