sql - query a json key in postgres json field -
say have in postgres stored in json field called “data” this
{ "custa": { "name": "customer a", }, "custb": { "name": "customer b", }, "custc": { "name": "customer c", } }
how can query return record contains key “custa” ? or better value of “custa” "name": "customer a"
trying cant use keyword key
select * invoices data->>key = 'custa';
select '{ "custa": { "name": "customer a" }, "custb": { "name": "customer b" }, "custc": { "name": "customer c" } }'::json#>>'{custa}'; ?column? ------------------------------ { + "name": "customer a"+ } (1 row)
note: have trailing commas after name:customer x, not proper json. query, like:
select data#>>'{custa}' invoices;
or, if data isn't json field:
select data::json#>>'{custa}' invoices;
i don't understand why invoice have more 1 customer though.
-g
Comments
Post a Comment