Postgres jsonb_object() function
Creates a JSONB object from key-value pairs
The jsonb_object
function in Postgres is used to create a JSONB
object from a set of key-value pairs. It is particularly useful when you need to generate JSONB
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 JSONB
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
jsonb_object
Creating nested JSON objects with You could use jsonb_object
to create nested JSONB
objects for representing more complex data. However, since jsonb_object
only expects text values for each key, we will need to combine it with other JSONB
functions like jsonb_build_object
. For example:
This query returns the following result:
Additional considerations
Gotchas
- 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
jsonb_object
expects text arrays, you may need to explicitly cast non-text data types to text.
Alternative options
- json_object - Same functionality as
jsonb_object
, but returns aJSON
object instead ofJSONB
. - to_jsonb - It can be used to create a
JSONB
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 thanjsonb_object
since all fields in the row are included in theJSONB
object. - jsonb_build_object - Similar to
jsonb_object
, but allows for more flexibility in constructing theJSONB
object, as it can take a variable number of arguments in the form of key-value pairs. - jsonb_object_agg - It is used to aggregate the key-value pairs from multiple rows into a single
JSONB
object. In contrast,jsonb_object
outputs aJSONB
object for each row.
Resources
Last updated on