I'm trying to query a JSON data but am running int...
# help
c
I'm trying to query a JSON data but am running into the following issue. Looking at the docs it seems that this should be doable. Query:
Copy code
await supabase
    .from<definitions['pass']>('pass')
    .select(
      'id, json',
      {
        count: 'exact',
      }
    )
    .lt('json->expirationDate', new Date(now).toISOString().toLocaleString())
Error:
Copy code
{
  message: 'invalid input syntax for type json',
  code: '22P02',
  hint: null,
  details: 'Token "-02" is invalid.'
}
s
What does the data inside of the
json
column look like?
If you are comparing the
json
expirationDate to a string you will probably need double
>
instead.
Copy code
js
await supabase
    .from<definitions['pass']>('pass')
    .select(
      'id, json',
      {
        count: 'exact',
      }
    )
    .lt('json->>expirationDate', new Date(now).toISOString().toLocaleString())
c
Copy code
{
  "generic": {
    "backFields": [
      {
        "key": "status",
        "label": "Status",
        "value": "Valid"
      }
    ],
    "headerFields": [],
    "auxiliaryFields": [],
    "secondaryFields": []
  },
  "barcodes": [],
  "logoText": "Logo Test",
  "labelColor": "rgb(69, 74, 117)",
  "formatVersion": 1,
  "expirationDate": "2022-02-15T16:33:28Z",
  "teamIdentifier": "G496R7LJ3Q",
  "backgroundColor": "rgb(255 ,255 , 255)",
  "foregroundColor": "rgb(69, 74, 117)"
}
looks like that might be it
s
We should probably document that in our docs, but you can find out more about stuff like this from the postgrest website https://postgrest.org/en/v7.0.0/api.html#json-columns
c
query isn't failing anymore but need to double check the results
What would be the best to check if the fields exists at all?
Since it's value is never set to null, it either exists or doesn't
s
I would like to think the
.is
filter, but since this field is a from a
jsonb
column, not sure if it will work the same. But you can give it a try
c
will do. I think using
lt
should cover that case for me
Looks like it's working. Thank you so much!
s
You're welcome.
c
.is('json->>expirationDate', null)
Looks like that works
s
Ah that's good to know
c
I'm probably pushing things a bit here but this does not work.
Copy code
.or('json->>expirationDate.is.null', `json->>expirationDate.lt.${new Date(now).toISOString().toLocaleString()}`)
seems to only apply the first filter
my bad, didn't realize the entire input was a single string.
Copy code
.or(`json->>expirationDate.is.null,json->>expirationDate.lt.${new Date(now).toISOString().toLocaleString()}`)
^^ This works
s
It's good that you are experimenting and finding out what works and what doesn't.