/supabase-tutorials

How to store JSON data in Supabase?

Learn how to store JSON data in Supabase. Follow our step-by-step guide to set up your project, configure a jsonb column, insert and query JSON, and secure your keys.

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 store JSON data in Supabase?

 

Step 1: Set Up a Supabase Project

 

First, you need to create a new project on Supabase:

  • Visit the Supabase website.
  • Log in or sign up for an account if you haven't yet.
  • Once logged in, click on "New Project."
  • Fill in the details like the project name and choose a database password.
  • Click "Create" to set up your new project.

 

Step 2: Configure the Database

 

To store JSON data, you need to configure your database:

  • After your project is created, navigate to the "Database" section.
  • Go to "Table Editor" and click on "New Table."
  • Provide a name for your table.
  • Add a column specifically for JSON data, ensuring it's of type jsonb to store JSON efficiently.

For example, if your table is named data_storage, you might add:


{
  "column_name": "json_data",
  "data\_type": "jsonb"
}

 

Step 3: Install Supabase Client

 

Use the Supabase JavaScript client to interact with your database:

  • Make sure you have Node.js installed.
  • Initialize a new Node.js project using:

npm init -y
  • Install the Supabase client via npm:

npm install @supabase/supabase-js

 

Step 4: Connect to Your Supabase Project

 

Set up a connection to your Supabase project in your Node.js file:


const { createClient } = require('@supabase/supabase-js');

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

Replace 'https://your-project-ref.supabase.co' and 'your-anon-key' with your actual Supabase project URL and the anon key, both found in the project's API settings.

 

Step 5: Insert JSON Data

 

To insert JSON data into your table, use the following example:


const insertJsonData = async () => {
  const { data, error } = await supabase
    .from('data\_storage')
    .insert([
      { json\_data: { key1: 'value1', key2: 'value2' } }
    ]);

  if (error) {
    console.error('Error inserting data:', error);
  } else {
    console.log('Data inserted:', data);
  }
};

insertJsonData();

 

Step 6: Query JSON Data

 

You can also query JSON data with the Supabase client:


const queryJsonData = async () => {
  const { data, error } = await supabase
    .from('data\_storage')
    .select('\*');

  if (error) {
    console.error('Error querying data:', error);
  } else {
    console.log('Queried data:', data);
  }
};

queryJsonData();

 

Step 7: Use Environment Variables for Security

 

For better security, store your keys in environment variables rather than hard-coding them. Use a package like dotenv:


npm install dotenv

Create a .env file and add your Supabase credentials:

SUPABASE_URL=https://your-project-ref.supabase.co
SUPABASE_ANON_KEY=your-anon-key

Load these variables in your Node.js application:


require('dotenv').config();

const supabaseUrl = process.env.SUPABASE\_URL;
const supabaseKey = process.env.SUPABASE_ANON_KEY;
// Use these variables in createClient

 

Conclusion

 

By following these steps, you set up a project on Supabase, configured a table to store JSON data, installed and configured the Supabase client, and performed operations to insert and query JSON data. Secure your credentials using environment variables to maintain the security of your application.

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