Skip to content

Instantly share code, notes, and snippets.

@itspb
Last active September 8, 2023 20:31
Show Gist options
  • Save itspb/5625e223395fce674402bb500887be6f to your computer and use it in GitHub Desktop.
Save itspb/5625e223395fce674402bb500887be6f to your computer and use it in GitHub Desktop.
DROP TABLE IF EXISTS purchases;
CREATE TEMP TABLE purchases (
data jsonb
);
INSERT INTO purchases (data)
VALUES ('{
"purchase_id": 83577474,
"customer_id": 173645,
"items": [
{
"product_id": 76,
"title": "beer",
"price": 9.99,
"quantity": 6
},
{
"product_id": 90,
"title": "pampers",
"price": 545.60,
"quantity": 2
}
],
"total_price": 1151.14
}
')
;
SELECT data->>'customer_id' AS customer_id
FROM purchases
;
SELECT (jsonb_array_elements(data->'items')->>'title') AS title
FROM purchases
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment