MySQL includes an aggregate function called JSON_OBJECTAGG()
. This function enables you to create a JSON object containing key-value pairs. More specifically, it lets you create this JSON object based on the results of a query.
It accepts two arguments, the first of these being used as a key and the second as a value. These arguments can be column names or expressions.