Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Adds the json doc back - updates it

Copple8a3aad72913a
diff --git a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
index e22da537402..4203846a784 100644
--- a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
+++ b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
@@ -526,6 +526,10 @@ export const database: NavMenuConstant = {
name: 'Postgres Guides',
url: undefined,
items: [
+ {
+ name: 'JSON and unstructured data',
+ url: '/guides/database/json',
+ },
{ name: 'Implementing Full Text Search', url: '/guides/database/full-text-search' },
{ name: 'Implementing Cascade Deletes', url: '/guides/database/postgres/cascade-deletes' },
{ name: 'Implementing column encryption', url: '/guides/database/column-encryption' },
diff --git a/apps/docs/pages/guides/database/json.mdx b/apps/docs/pages/guides/database/json.mdx
index 60bbda028ee..46e60985db4 100644
--- a/apps/docs/pages/guides/database/json.mdx
+++ b/apps/docs/pages/guides/database/json.mdx
@@ -2,45 +2,36 @@ import Layout from '~/layouts/DefaultGuideLayout'
export const meta = {
id: 'json',
- title: 'JSON',
- description: 'Using the JSON data type in PostgreSQL.',
+ title: 'Managing JSON and unstructured data',
+ description: 'Using the JSON data type in Postgres.',
+ subtitle: 'Using the JSON data type in Postgres.',
}
-PostgreSQL supports [JSON functions and operators](https://www.postgresql.org/docs/current/functions-json.html) which gives flexibility when storing data inside a database column.
+Postgres supports storing and querying unstructured data.
-PostgreSQL supports two types of JSON columns: `JSON` and `JSONB`.
-The recommended type is `JSONB` for almost all cases.
-When you use the `JSONB` format, the data is parsed when it's put into the database so it's faster when querying and also it can be indexed.
+## JSON vs JSONB
-## Create a table with a JSON column
+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.
+
+- `json` stores an exact copy of the input text. Database functions must reparse the content on each execution.
+- `jsonb` stores 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:
<Tabs
scrollable
size="small"
type="underlined"
- defaultActiveId="dashboard"
+ defaultActiveId="sql"
>
-<TabPanel id="dashboard" label="Dashboard">
-
-1. Go to the [Table Editor](https://app.supabase.com/project/_/editor) page in the Dashboard.
-2. Click **New Table** and create a table called `books`.
-3. Include a primary key with the following properties:
- - Name: `id`
- - Type: `int8`
- - Default value: `Automatically generate as indentity`
-4. Click **Save**.
-5. Click **New Column** and add 3 columns with the following properties:
- - **title** column
- - Name: `title`
- - Type: `text`
- - **author** column
- - Name: `author`
- - Type: `text`
- - **metadata** column
- - Name: `metadata`
- - Type: `jsonb`
-
-</TabPanel>
<TabPanel id="sql" label="SQL">
```sql
@@ -52,32 +43,39 @@ create table books (
);
```
+</TabPanel>
+<TabPanel id="dashboard" label="Dashboard">
+
+1. Go to the [Table Editor](https://app.supabase.com/project/_/editor) page in the Dashboard.
+2. Click **New Table** and create a table called `books`.
+3. Include a primary key with the following properties and click save:
+
+- Name: `id`
+ - Type: `int8`
+ - Default value: `Automatically generate as indentity`
+- **title** column
+ - Name: `title`
+ - Type: `text`
+- **author** column
+ - Name: `author`
+ - Type: `text`
+- **metadata** column
+ - Name: `metadata`
+ - Type: `jsonb`
+
</TabPanel>
</Tabs>
-## Insert data into the table
+## Inserting JSON data
+
+You can insert JSON data in the same way that you insert any other data. The data must be valid JSON.
<Tabs
scrollable
size="small"
type="underlined"
- defaultActiveId="dashboard"
+ defaultActiveId="sql"
>
-<TabPanel id="dashboard" label="Dashboard">
-
-1. Go to the [Table Editor](https://app.supabase.com/project/_/editor) page in the Dashboard.
-2. Select the `books` table in the sidebar.
-3. Click **+ Insert row** and add 5 rows with the following properties:
-
-| id | title | author | metadata |
-| --- | ----------------------------------- | ---------------------- | --------------------------------------------------------------------------------------------------------------------- |
-| 1 | The Poky Little Puppy | Janette Sebring Lowrey | `json {"ages":[3,6],"price":5.95,"description":"Puppy is slower than other, bigger animals."}` |
-| 2 | The Tale of Peter Rabbit | Beatrix Potter | `json {"ages":[2,5],"price":4.49,"description":"Rabbit eats some vegetables."}` |
-| 3 | Tootle | Gertrude Crampton | `json {"ages":[2,5],"price":3.99,"description":"Little toy train has big dreams."}` |
-| 4 | Green Eggs and Ham | Dr. Seuss | `json {"ages":[4,8],"price":7.49,"description":"Sam has changing food preferences and eats unusually colored food."}` |
-| 5 | Harry Potter and the Goblet of Fire | J.K. Rowling | `json {"ages":[10,99],"price":24.95,"description":"Fourth year of school starts, big drama ensues."}` |
-
-</TabPanel>
<TabPanel id="sql" label="SQL">
```sql
@@ -111,6 +109,21 @@ values
);
```
+</TabPanel>
+<TabPanel id="dashboard" label="Dashboard">
+
+1. Go to the [Table Editor](https://app.supabase.com/project/_/editor) page in the Dashboard.
+2. Select the `books` table in the sidebar.
+3. Click **+ Insert row** and add 5 rows with the following properties:
+
+| id | title | author | metadata |
+| --- | ----------------------------------- | ---------------------- | --------------------------------------------------------------------------------------------------------------------- |
+| 1 | The Poky Little Puppy | Janette Sebring Lowrey | `json {"ages":[3,6],"price":5.95,"description":"Puppy is slower than other, bigger animals."}` |
+| 2 | The Tale of Peter Rabbit | Beatrix Potter | `json {"ages":[2,5],"price":4.49,"description":"Rabbit eats some vegetables."}` |
+| 3 | Tootle | Gertrude Crampton | `json {"ages":[2,5],"price":3.99,"description":"Little toy train has big dreams."}` |
+| 4 | Green Eggs and Ham | Dr. Seuss | `json {"ages":[4,8],"price":7.49,"description":"Sam has changing food preferences and eats unusually colored food."}` |
+| 5 | Harry Potter and the Goblet of Fire | J.K. Rowling | `json {"ages":[10,99],"price":24.95,"description":"Fourth year of school starts, big drama ensues."}` |
+
</TabPanel>
<TabPanel id="js" label="JavaScript">
@@ -167,48 +180,11 @@ const { data, error } = await supabase.from('books').insert([
</TabPanel>
</Tabs>
-## View the data
-
-<Tabs
- scrollable
- size="small"
- type="underlined"
- defaultActiveId="sql"
->
-<TabPanel id="sql" label="SQL">
-
-```sql
-select *
-from books;
-```
-
-</TabPanel>
-<TabPanel id="js" label="JavaScript">
-
-```js
-const { data, error } = await supabase.from('books').select('*')
-console.log(JSON.stringify(data, null, 2))
-```
-
-</TabPanel>
-<TabPanel id="result" label="Result">
-
-| id | title | author | metadata |
-| --- | ----------------------------------- | ---------------------- | --------------------------------------------------------------------------------------------------------------------- |
-| 1 | The Poky Little Puppy | Janette Sebring Lowrey | `json{"ages":[3,6],"price":5.95,"description":"Puppy is slower than other, bigger animals."}` |
-| 2 | The Tale of Peter Rabbit | Beatrix Potter | `json{"ages":[2,5],"price":4.49,"description":"Rabbit eats some vegetables."} ` |
-| 3 | Tootle | Gertrude Crampton | `json{"ages":[2,5],"price":3.99,"description":"Little toy train has big dreams."} ` |
-| 4 | Green Eggs and Ham | Dr. Seuss | `json{"ages":[4,8],"price":7.49,"description":"Sam has changing food preferences and eats unusually colored food."} ` |
-| 5 | Harry Potter and the Goblet of Fire | J.K. Rowling | `json{"ages":[10,99],"price":24.95,"description":"Fourth year of school starts, big drama ensues."} ` |
-
-The data as it appears here has the `JSONB` fields in a different order than when inserted. As mentioned earlier, data is parsed as its inserted when using the JSONB format.
-
-</TabPanel>
-</Tabs>
+## Query JSON data
-## Query the `JSONB` data
+Querying JSON data is similar to querying other data, with a few other features to access nested values.
-Select the title, description, price, and age range for each book.
+Postgres support a range of [JSON functions and operators](https://www.postgresql.org/docs/current/functions-json.html). For example, the `->` operator returns values as `jsonb` data. If you want the data returned as `text`, use the `->>` operator.
<Tabs
scrollable
@@ -221,7 +197,7 @@ Select the title, description, price, and age range for each book.
```sql
select
title,
- metadata -> 'description' as description,
+ metadata ->> 'description' as description, -- returned as text
metadata -> 'price' as price,
metadata -> 'ages' -> 0 as low_age,
metadata -> 'ages' -> 1 as high_age
@@ -232,12 +208,13 @@ from books;
<TabPanel id="js" label="JavaScript">
```js
-const { data, error } = await supabase
- .from('books')
- .select(
- 'title,description:metadata->description,price:metadata->price,low_age:metadata->ages->0,high_age:metadata->ages->1'
- )
-console.log(JSON.stringify(data, null, 2))
+const { data, error } = await supabase.from('books').select(`
+ title,
+ description: metadata->description,
+ price: metadata->price,
+ low_age: metadata->ages->0,
+ high_age: metadata->ages->1
+ `)
```
</TabPanel>
@@ -254,16 +231,42 @@ console.log(JSON.stringify(data, null, 2))
</TabPanel>
</Tabs>
-Note that the `->` operator returns JSONB data. If you want TEXT/STRING data returned, use the `->>` operator.
+## Validating JSON data
+
+Supabase provides the [`pg_jsonschema` extension](/docs/guides/database/extensions/pg_jsonschema) that adds the ability to validate `json` and `jsonb` data types against [JSON Schema](https://json-schema.org/) documents.
-- metadata -> 'description' (returns a JSON object)
-- metadata ->> 'description' (returns STRING/TEXT data)
+Once you have enabled the extension, you can add a "check constraint" to your table to validate the JSON data:
+
+```sql
+create table customers (
+ id serial primary key,
+ metadata json
+);
+
+alter table customers
+add constriant check_metadata check (
+ json_matches_schema(
+ '{
+ "type": "object",
+ "properties": {
+ "tags": {
+ "type": "array",
+ "items": {
+ "type": "string",
+ "maxLength": 16
+ }
+ }
+ }
+ }',
+ metadata
+ )
+);
+```
## Resources
-- [Supabase JS Client](https://github.com/supabase/supabase-js)
-- [PostgreSQL: JSON Functions and Operators](https://www.postgresql.org/docs/12/functions-json.html)
-- [PostgreSQL JSON types](https://www.postgresql.org/docs/12/datatype-json.html)
+- [Postgres: JSON Functions and Operators](https://www.postgresql.org/docs/current/functions-json.html)
+- [Postgres JSON types](https://www.postgresql.org/docs/current/datatype-json.html)
export const Page = ({ children }) => <Layout meta={meta} children={children} />