Postgres json_object() function
Creates a JSON object from key-value pairs
The json_object
function in Postgres is used to create a JSON
object from a set of key-value pairs. It is particularly useful when you need to generate JSON
data dynamically from existing table data or input parameters.
Function signature
This function takes two text arrays as input: one for keys and one for values. Both arrays must have the same number of elements, as each key is paired with the corresponding value to construct the JSON
object.
Alternatively, you can pass a single text array containing both keys and values. In this case, alternate elements in the array are treated as keys and values, respectively.
Example usage
Consider a scenario where you run a library and have a table that tracks details for each book.
The table with some sample data can be set up as shown:
When querying this dataset, the frontend client might want to present the data in a different way. Say you want the catalog information just as the list of book names while combining the rest of the fields into a single metadata
attribute. You can do so as shown here:
This query returns the following result:
Advanced examples
json_object
Creating nested JSON objects with You could use json_object
to create nested JSON
objects for representing more complex data. However, since json_object
only expects text values for each key, we will need to combine it with other JSON
functions like json_build_object
. For example:
This query returns the following result:
Additional considerations
Gotchas and footguns
- Ensure both keys and values arrays have the same number of elements. Mismatched arrays will result in an error. Or, if passing in a single key-value array, ensure that the array has an even number of elements.
- Be aware of data type conversions. Since
json_object
expects text arrays, you may need to explicitly cast non-text data types to text.
Alternative functions
- jsonb_object - Same functionality as
json_object
, but returns aJSONB
object instead ofJSON
. - row_to_json - It can be used to create a
JSON
object from a table row (or a row of a composite type) without needing to specify keys and values explicitly. Although, it is less flexible thanjson_object
since all fields in the row are included in theJSON
object. - json_build_object - Similar to
json_object
, but allows for more flexibility in constructing theJSON
object, as it can take a variable number of arguments in the form of key-value pairs. - json_object_agg - It is used to aggregate the key-value pairs from multiple rows into a single
JSON
object. In contrast,json_object
outputs aJSON
object for each row.