I want that MySQL will return a result set in a form of JSON array.
A worked query:
SELECT CONCAT(
'[',
GROUP_CONCAT(
JSON_OBJECT(
'ProductId', tblproducts.ProductId,
'ProductName', tblproducts.ProductName
)
),
']') As Products
FROM tblproducts;
This query returns the following JSON array:
[
{
"ProductId": "1",
"ProductName": "Milk"
},
{
"ProductId": "2",
"ProductName": "Bread"
}
]
Although this code works, I have a strong feeling that building a JSON array with GROUP_CONCAT and CONCAT is kind of workaround. If to use a JSON_ARRAY, a result set will consist of JSON arrays for each JSON_OBJECT.
Is there any native way to get a single JSON array with all JSON_OBJECT in the result set?