Max
Max

Reputation: 19

how to access one key inside jsonb field?

Suppose I have a table like this

     Column      │         Type         │                             Modifiers
─────────────────┼──────────────────────┼────────────────────────────────────────────────────────────────────
 id              │ integer              │ not null default
 practice_id     │ character varying(6) │ not null
 date            │ date                 │ not null
 pct_id          │ character varying(3) │
 astro_pu_items  │ double precision     │ not null
 astro_pu_cost   │ double precision     │ not null
 total           │ jsonb                │

I need to access total field and find the particular key. So if the key name is sub_total how can I find through query. I'm using Postgres 14.1.

I'm not able to find postgres function which can do that. So guide me here

Upvotes: 0

Views: 264

Answers (1)

Don Ho
Don Ho

Reputation: 1299

Use the -> Operator.

select total->'sub_total' from tablename;

https://www.postgresql.org/docs/current/functions-json.html

Upvotes: 2

Related Questions