How to select a subset of fields from a JSONB?
I'm using Postgres and can't figure out how to select a subset of JSONB fields? Is this supported yet?
11 Replies
There are JSON operators in PG that you can use
PostgreSQL Documentation
JSON Functions and Operators
Thanks @Angelelz. I'm aware of the
->>
operator which is how I handle this outside of Drizzle. I have several working examples of this but Drizzle doesn't seem to like this syntax. What am I doing wrong here assuming data
is a JSONB type and the intention is to pull out just the agency
field?
Tried this as well with no luck.
Oh, that's just cause you use the sql operator per column:
Or how you'd usually see it in drizzle:
BTW, @rphlmr ⚡ is an expert in JSON data in postgres, he has a couple helpers for this type of stuff
Thanks again @Angelelz. The above two syntaxes didn't work but the one below inspired by your reply actually worked. @rphlmr ⚡ would you be able to help here? Is there an easy way to not have to spell out each field individually?
This is from Supabase and it returns the selected fields keeping the data field as an object.
Expected return format
This is not my area, I've never been a fan of saving json in a relational db. But I think I found some of the stuff that he shared: https://gist.github.com/rphlmr/0d1722a794ed5a16da0fdf6652902b15#file-utils-ts-L60
Thanks for finding and sharing this. Some pretty cool stuff here!
hey sorry it was bed time for me 😄
here an example: https://drizzle.run/wgnd28shd0f8hpmhyxnpisew
Drizzle Run
Select field in jsonb - Drizzle Run
note de
()
and the string interpolation on your table.field
->
=> get the value
->>
=> get the value as stringThanks @rphlmr ⚡ that's exactly what I ended up with based on the feedback I was provided. Your GitHub gist had some really cool functions in it!