Building JSON in a query in SQL
The other direction: rows in, one JSON document out.
SELECT
JSON_OBJECT(
'id', id,
'sku', sku,
'price', price,
'labels', JSON_ARRAY('catalog', 'active'),
'nested', JSON_OBJECT('currency', 'USD')
) AS document
FROM products
ORDER BY id;
How it works
json_objecttakes alternating keys and values.json_arraytakes a list; both nest inside each other freely.json_quotewraps a single SQL value as valid JSON.
Keywords and builtins used here
ASBYFROMORDERSELECT
The run, in numbers
- Lines
- 10
- Characters to type
- 182
- Tokens
- 41
- Three-star pace
- 95 tpm
At the three-star pace of 95 tokens a minute, this run takes about 26 seconds.
Step 1 of 5 in Writing JSON, step 4 of 14 in JSON & search.