/supabase-tutorials

How to query JSON fields in Supabase?

Discover how to query JSON fields in Supabase with our step-by-step guide. Learn to set up projects, create jsonb tables, insert data, and run SQL queries.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to query JSON fields in Supabase?

 

Step 1: Set Up Your Supabase Project

 

To begin querying JSON fields in Supabase, you first need to set up your Supabase project:

  1. Go to the Supabase website.
  2. Sign in to your account or create a new one.
  3. Create a new project and name it appropriately.

 

Step 2: Create a Table with JSON Field

 

Create a table that contains a JSON field:

In your Supabase dashboard:

  • Navigate to the "Table Editor" on the left sidebar.
  • Click "New Table" and name your table (e.g., users).
  • Add columns to your table. Ensure you add at least one column with the data type jsonb to store JSON data.

For example:

  • id: integer (Primary Key, Auto-increment)
  • name: text
  • metadata: jsonb

After setting up the columns, click "Save" to create your table.

 

Step 3: Insert Data into JSON Field

 

To insert data into the JSON field, you can use the SQL editor within the Supabase dashboard:

Open the SQL editor, and enter the following SQL command to insert data into your users table:


INSERT INTO users (name, metadata) 
VALUES ('Jane Doe', '{"age": 28, "occupation": "developer"}');

Run the query to insert the data.

 

Step 4: Query JSON Fields

 

You can query the JSON fields using SQL queries. Here are some examples:

To select all rows where the metadata includes the occupation of 'developer,' use:


SELECT \* FROM users WHERE metadata->>'occupation' = 'developer';

To extract specific keys from the JSON field, you can use:


SELECT metadata->>'age' AS age FROM users;

This query extracts the age from the JSON field for all users.

 

Step 5: Update Data in JSON Field

 

To update data within the JSON field, use the following approach:

If you wish to update the 'age' for a specific user, you can execute:


UPDATE users 
SET metadata = jsonb\_set(metadata, '{age}', '30') 
WHERE name = 'Jane Doe';

This command updates Jane Doe's age to 30.

 

Step 6: Use Supabase Client for JSON Queries

 

For interacting programmatically, use the Supabase client. Here's an example using JavaScript:

Install the Supabase client:


npm install @supabase/supabase-js

Then, use the client to query JSON data:


import { createClient } from '@supabase/supabase-js'

const supabaseUrl = 'https://your-project-ref.supabase.co'
const supabaseKey = 'your-anon-key'
const supabase = createClient(supabaseUrl, supabaseKey)

const { data, error } = await supabase
  .from('users')
  .select()
  .eq('metadata->>occupation', 'developer')

Replace 'your-project-ref' and 'your-anon-key' with your actual Supabase project credentials.

 

Conclusion

 

You now have a comprehensive understanding of querying JSON fields in Supabase. From setting up the project and creating a table to inserting, querying, updating data, and using the Supabase client, this guide helps you manage JSON data effectively within Supabase. Remember to replace placeholder values with your actual project details when implementing.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022