PostgreSQL jsonb_to_record() Function
Summary: in this tutorial, you will learn how to use the PostgreSQL jsonb_to_record()
function to convert a JSON object into a PostgreSQL record type.
Introduction to the PostgreSQL JSONB jsonb_to_record() function
The jsonb_to_record()
function allows you to convert a JSON object into a PostgreSQL record type.
Here’s the basic syntax of the jsonb_to_record()
function:
In this syntax:
- First, specify a JSON object (
json_object
) of type JSONB that you want to convert into a record type. - Second, define the
record_type
with a list of columns that have the same names as the keys in the JSON object. The record may have fewer columns than the number of keys in the JSON object. If the names of the columns of therecord_type
are not the same as the names of the keys in the JSON object, they will be ignored.
The function jsonb_to_record()
returns a record type representing the structure of a JSON object.
If the json_object
is null, the jsonb_to_record()
function returns null.
PostgreSQL jsonb_to_record() function examples
Let’s take some examples of using the jsonb_to_record()
function.
1) Basic PostgreSQL jsonb_to_record() function examples
The following example uses the jsonb_to_record()
function to convert a JSON object into a record:
Output:
The following example converts a JSON object into a record type but with fewer keys:
Output:
2) Using jsonb_to_record() function with user-defined type
First, create a new custom type called pet
with two fields type
and name
:
Second, use the pet type with the jsonb_to_record()
function:
Output:
Summary
- Use the
jsonb_to_record()
function to convert a JSON object into a PostgreSQL record type.