PostgreSQL® offers two types of data types to handle JSON data, JSON
and JSONB
, you can use the function json_object
(jsonb_object
for JSONB
) to create a JSON object from an array of key/value pairs.
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
Create a JSON object from an arrays of keys/values pairs with the json_object
function
You can create a JSON object from two arrays (one of keys and one of values) with the json_object
(jsonb_object
for JSONB) function
select
jsonb_object(
'{{name, Francesco}, {pizza, Margherita}}');
Result
jsonb_object
----------------------------------------------
{"name": "Francesco", "pizza": "Margherita"}
(1 row)
Review all the JSON PostgreSQL use-cases listed in the main page