Search code examples
sqljsonstringpostgresqlmetabase

PostgreSQL - Converting a string with quotes to different string using case?


So hopefully this isn't a super difficult question. I've looked around but haven't been able to find an answer. Basically I have a table in a READ-ONLY db that I'm trying to use a case statement to convert a column that stores JSON values (I think?) to something more visually pleasing.

The table shows Emails, and the Status of whether or not they're subscribed to the mailing list.

EXAMPLE HERE

Basically my query looks something like this

select
    f.data as "Email",
    (
     case f.status
        when '{"value":true}' then 'Yes'
        when '{"value":false}' then 'No'
        else NULL
     end
     ) as "Subscribed"
from fields f

When I run this in my example page it works just fine when set to POSTGRES 11 but when I run it on Metabase, I get an error "ERROR: operator does not exist: json = unknown" and I'm stumped on how to proceed.

Any help here would be greatly appreciated.


Solution

  • That error means f.status is of type json and you're trying to compare it to a string, which doesn't work.

    You can try the following instead (related documentation):

    case f.status->>'value'
        when 'true' then 'Yes'
        when 'false' then 'No'
    end
    as "Subscribed"