How to build a JSON array from a list of elements in PostgreSQL®
- One minute read - 180 wordsPostgreSQL® offers two types of data types to handle JSON data, JSON
and JSONB
, you can use the function json_build_array
(jsonb_build_array
for JSONB
) to create a JSON array from a list of elements.
NOTE: To review the differences between
JSON
andJSONB
check out the related article.
NOTE: more info is available in the PostgreSQL JSON functions documentation page
👉 Want a FREE PostgreSQL database?👈
🦀 Check Aiven’s FREE plans! 🦀
⚡️ Want to optimize your SQL query with AI? ⚡️
Check Aiven SQL Query Optimizer!
⚡️ Want a Fully PostgreSQL optimized Database? ⚡️
Check Aiven AI Database Optimizer!
Powered by EverSQL, it provides you index and SQL rewrite recommendations to take your database performance to the next level
Create a JSON array from a list of elements with the json_build_array
function
You can create a JSON array from a list of elements with the json_build_array
(jsonb_build_array
for JSONB) function
select
jsonb_build_array(1,4,'🍌', 'pizza');
Result
jsonb_build_array
-----------------------
[1, 4, "🍌", "pizza"]
(1 row)
NOTE: the array elements can be heterogeneously-typed
Review all the JSON PostgreSQL use-cases listed in the main page