I have a jsonb column in one of the tables that has the following structure:
{
"3424": {
"status": "pending",
"remarks": "sample here"
},
"6436": {
"status": "new",
"remarks": "sample here"
},,
"9768": {
"status": "cancelled",
"remarks": null,
"by": "customer"
}
}
I am trying to create a view that will put the statuses in individual columns and the key will be their value:
pending | new | cancelled | accepted | id | transaction
3424 | 6436 | 9768 | null | 1 | testing
The problem is the key is dynamic (numeric and corresponds to some id) so I cannot pinpoint the exact key to use the functions/operations stated here: https://www.postgresql.org/docs/9.5/functions-json.html
I've read about json_path_query and was able to extract the statuses here without the need to know the key but I cannot combine it with the integer key yet.
select mt.id, mt.transaction, hstatus from mytable mt
cross join lateral jsonb_path_query(mt.hist, '$.**.status') hstatus
where mt.id = <id>
but this returns the statuses as rows for now. I'm pretty noob with (postgre)sql so I've only gotten this far.
pending? Or three withcancelled?