I have a table in postgres called day, which contains a jsonb column called plan_activities.
I have a day record with day.id = 18 and plan_activities contains the following JSON:
[
{
"activity": "Gym",
"cardio": false,
"strength": true,
"quantity": 20,
"units": "mins",
"timeOfDay": "Evening",
"summary": "Gym - 20 mins - Evening",
"timeOfDayOrder": 4
},
{
"activity": "Walk",
"cardio": true,
"strength": false,
"quantity": 15,
"units": "minutes",
"timeOfDay": "morning",
"summary": "Walk - 15 minutes - Lunchtime",
"timeOfDayOrder": 1
}
]
When I execute the following query:
select jsonb_array_elements(day.plan_activities) as activities
from day
where day.id = 18;
I get the following error:
Failed to run sql query: cannot extract elements from a scalar
The JSON contains a valid JSON array as far as I can tell. What am I doing wrong?
My eventual goal if I can extract this list is to create separate records elsewhere, each of which contains all the fields plus a reference back to the day record.