Managing JSON and unstructured data
Using the JSON data type in Postgres.
Postgres supports storing and querying unstructured data.
JSON vs JSONB
Postgres supports two types of JSON columns: json (stored as a string) and jsonb (stored as a binary). The recommended type is jsonb for almost all cases.
jsonstores an exact copy of the input text. Database functions must reparse the content on each execution.jsonbstores database in a decomposed binary format. While this makes it slightly slower to input due to added conversion overhead, it is significantly faster to process, since no reparsing is needed.
When to use JSON/JSONB
Generally you should use a jsonb column when you have data that is unstructured or has a variable schema. For example, if you wanted to store responses for various webhooks, you might not know the format of the response when creating the table. Instead, you could store the payload as a jsonb object in a single column.
Don't go overboard with json/jsonb columns. They are a useful tool, but most of the benefits of a relational database come from the ability to query and join structured data, and the referential integrity that brings.
Create JSONB columns
json/jsonb is just another "data type" for Postgres columns. You can create a jsonb column in the same way you would create a text or int column:
123456create table books ( id serial primary key, title text, author text, metadata jsonb);Inserting JSON data
You can insert JSON data in the same way that you insert any other data. The data must be valid JSON.
12345678910111213141516171819202122232425262728insert into books (title, author, metadata)values ( 'The Poky Little Puppy', 'Janette Sebring Lowrey', '{"description":"Puppy is slower than other, bigger animals.","price":5.95,"ages":[3,6]}' ), ( 'The Tale of Peter Rabbit', 'Beatrix Potter', '{"description":"Rabbit eats some vegetables.","price":4.49,"ages":[2,5]}' ), ( 'Tootle', 'Gertrude Crampton', '{"description":"Little toy train has big dreams.","price":3.99,"ages":[2,5]}' ), ( 'Green Eggs and Ham', 'Dr. Seuss', '{"description":"Sam has changing food preferences and eats unusually colored food.","price":7.49,"ages":[4,8]}' ), ( 'Harry Potter and the Goblet of Fire', 'J.K. Rowling', '{"description":"Fourth year of school starts, big drama ensues.","price":24.95,"ages":[10,99]}' );Query JSON data
Querying JSON data is similar to querying other data, with a few other features to access nested values.
Postgres support a range of JSON functions and operators. For example, the -> operator returns values as jsonb data. If you want the data returned as text, use the ->> operator.
1234567select title, metadata ->> 'description' as description, -- returned as text metadata -> 'price' as price, metadata -> 'ages' -> 0 as low_age, metadata -> 'ages' -> 1 as high_agefrom books;Validating JSON data
Supabase provides the pg_jsonschema extension that adds the ability to validate json and jsonb data types against JSON Schema documents.
Once you have enabled the extension, you can add a "check constraint" to your table to validate the JSON data:
1234567891011121314151617181920212223create table customers ( id serial primary key, metadata json);alter table customersadd constraint check_metadata check ( json_matches_schema( '{ "type": "object", "properties": { "tags": { "type": "array", "items": { "type": "string", "maxLength": 16 } } } }', metadata ));