Search code examples
javascriptjsonpostgresqlsupabase

Addressing JSON data in supabase JS client


I have a column guests which contains json data of the schema first_name, last_name. Each row can contain multiple guests.

[{"last_name":"One","first_name":"Guest"},
 {"last_name":"Two","first_name":"Guest"}]

I'm trying to address the data inside this object. In the supabase/postgres console the following statement gives me results as intended:

Select *
from bookings
where ((guests -> 0 ->> 'first_name') = 'Guest')

When querying the same in supabase JS it doesn't return any output:

let query = supabase.from("bookings")
                    .select()
                    .eq('guests -> 0 ->> first_name', 'Guest');

What am I missing here?

Also the statement to check if the json exists and is not empty doesn't work:

.not('guests -> 0 ->> first_name','is',null)

I tried different approaches but it seems I am addressing the json object in a wrong way.


Solution

  • Solution to the problem is to avoid whitespaces in the string:

    .eq('guests->0->>first_name','Guest')