I want to merge json data distributed over multiple rows in a mysql column via python sqlalchemy library. I was planning to use json_merge_preserve
but unable to use over an entire column.
I used following code:
select(func.json_merge_preserve(db.mytable.c.myjsoncolumn))
But it says that the function need at least two parameters.
2
Answers
I have used the sql logic mentioned in danblack's answer and implemented directly in python.
I am not sure of the performance implications of this solution.
An example is to create the SQL function:
Some sample data:
Use the function to return results:
Using the function from sql alchemeny is like this answer:
ref: fiddle
ref: aggregate functions of MariaDB
Note: MariaDB-10.3+ only, MySQL doesn’t have aggregate (non-UDF) functions.