I have a simple postgres table:
Column │ Type │ Modifiers
──────────────┼──────────────────────┼──────────────────────
id │ integer │ not null default
data │ jsonb │
Here's a simplified data structure for data
:
{
"id": 3,
"date": "2019-01-01",
"well_report_table":
[
{"element": "methane",
"yield": 6,
"price": 2.10
},
{"element": "pentane",
"yield": 6,
"price": 2.10
},
{"element": "butane",
"yield": 6,
"price": 3.50
}
],
"cost_report_table":
[
{"item": "fuel",
"charge": 6.30
},
{"item": "lease",
"charge": 200
}
]
}
I'd like to flatten this in a view with the following columns:
id | date | well_report_table_methane_yield | well_report_table_methane_price | well_report_table_pentane_yield | well_report_table_pentane_price | well_report_table_butane_yield | well_report_table_butane_price |cost_report_table_fuel_charge | cost_report_table_lease_charge
The objects in my arrays have an identifier that I would like to append to the array object name and then iterate through the other keys in the object and make columns out of .
This question gets me close: Postgres: Flatten aggregated key/value pairs from a JSONB field?
I'm not entirely sure this is possible in something like plpgsql, so if I just need to generate the view text in a scripting language like ruby/python and then create a view off of that, I'm a ok with that.
Ideally I'll be able to use something like jsonb_array_elements
and jsonb_each
in order to avoid intermediate tables (all of my current attempts have required intermediate views), but I haven't found that magic combination yet.
This is not a general question about flattening JSON arrays, because there is a specific logic hidden in the arrays. You can implement the logic in this function:
The query:
gives the object:
which can be converted to a tabular view in the way described in Flatten aggregated key/value pairs from a JSONB field?