MySQL has had a JSON data type since version 5.7 was released way back in '15. But did you know you could produce JSON output from non-JSON columns? It is very simple and saves a lot of time over trying to format it in your application.
World DatabaseWe will be using the good old World database that MySQL has used for years in documentation, examples, and in the classroom. Starting with a simple query we will build up to something more complex.
SELECT Name, District, Population FROM City;
This will output the data from the table in a tabular format.
'Kabul', 'Kabol', '1780000'
'Qandahar', 'Qandahar', '237500'
Array or Object?We have two options for composing JSON data: JSON_ARRAY and JSON_OBJECT.
Of the two, you will find JSON_ARRAY the least fussy. It will JSON-ize your data very easily. It takes a list of values or an empty list and returns a JSON array.
…[Read more]