Convert query result table to JSON array in MySQL

Sometimes small tricks are very handy. Mysql provides a function that creates JSON values for the row result.

mysql> SELECT JSON_OBJECT('id', 87, 'name', 'carrot');

Run query in PHP

SELECT json_object('name', name, 'phone', phone)
FROM person;

Check with MySQL documentation

Leave a Reply

Your email address will not be published. Required fields are marked *