Postgres jsonb_to_record() function
Convert a JSONB object to a record
You can use the jsonb_to_record
function to convert a top-level JSONB
object into a row, with the type specified by the AS
clause.
This function is useful when you need to parse JSONB
data received from external sources, such as APIs or file uploads, and store it in a structured format. By using jsonb_to_record
, you can easily extract values from JSONB
and map them to the corresponding columns in your database table.
Function signature
The function's definition includes a column definition list, where you specify the name and data type of each column in the resulting record.
Example usage
Consider a scenario in which you have JSONB
data representing employee information, and you want to ingest it for easier processing later. The JSONB
data looks like this:
The table you want to insert data into is defined as follows:
Using jsonb_to_record
, you can insert the input data into the employees
table as shown:
Note that the string representation of the JSON object didn't need to be explicitly cast to JSONB
. Postgres automatically casts it to JSONB
when the function is called.
To verify the data was inserted, you can run the following query:
This query returns the following result:
Advanced examples
This section provides advanced jsonb_to_record
examples.
jsonb_to_record
Handling partial data with For datapoints where the JSONB
objects have missing keys, jsonb_to_record
can still cast them into records, producing NULL
values for the unmatched columns. For example:
This query returns the following result:
jsonb_to_record
Handling nested data with jsonb_to_record
can also be used to handle nested JSONB
input data (i.e., keys with values that are JSONB
objects themselves). You need to first define a custom Postgres type. The newly created type can then be used in the column definition list along with the other columns.
In the following example, we handle the address
field by creating an ADDRESS_TYPE
type first.
This query returns the following result:
Alternative functions
-
jsonb_populate_record: This function can also be used to create records using values from a
JSONB
object. The difference is thatjsonb_populate_record
requires the record type to be defined beforehand, whilejsonb_to_record
needs the type definition inline. -
jsonb_to_recordset: This function can be used similarly to parse
JSONB
, the difference being that it returns a set of records instead of a single record. For example, if you have an array ofJSONB
objects, you can usejsonb_to_recordset
to convert each object into a new row. -
json_to_record: This function provides the same functionality as
json_to_record
, but acceptsJSON
input instead ofJSONB
. In cases where the input payload type isn't exactly specified, either of the two functions can be used.For example, take this
json_to_record
query:It works just as well as this
JSONB
variant (below) since Postgres casts the literalJSON
object toJSON
orJSONB
depending on the context.
Resources
Last updated on