SELECT
x.restored ->> 'date_restored' AS date_restored
FROM members a
CROSS JOIN LATERAL json_array_elements(a.data -> 'restoration_records') AS x(restored);
ERROR: cannot call json_array_elements on a scalar
Your error is happening because your data column is not a JSON array of restoration records, but instead an array of objects, and restoration_records is inside each object.
Here’s how to fix your query:
SELECT
r.value ->> 'date_restored' AS date_restored
FROM members a,
LATERAL json_array_elements(a.data) AS obj, -- loop over outer array
LATERAL json_array_elements(obj.value -> 'restoration_records') AS r; -- loop over restoration_records
Explanation:
a.data is a JSON array of objects → loop with json_array_elements.
Each object has a restoration_records array → loop again inside it.