patternsqlMajor
MySQL: Return JSON from a standard SQL Query
Viewed 0 times
returnsqlquerystandardmysqljsonfrom
Problem
I have read about JSON objects and the JSON object type. I only want to do a select and it return JSON. I do not necessarily want to store a JSON object. Serialization per se it not my question. The columns are regular Varchar, Int, etc. columns, no JSON Objects, "normal" database rows.
Can I do a regular old SELECT and return JSON for MySQL?
Isn't this what FOR JSON in SQL SERVER and rows_for_json do in PostgreSQL?
They seemed ahead in this are but I didn't want to fool myself.
I found this question from 2016:
https://stackoverflow.com/questions/35324795/mysql-5-7-return-row-as-json-using-new-json-features
Can I do a regular old SELECT and return JSON for MySQL?
Isn't this what FOR JSON in SQL SERVER and rows_for_json do in PostgreSQL?
They seemed ahead in this are but I didn't want to fool myself.
I found this question from 2016:
https://stackoverflow.com/questions/35324795/mysql-5-7-return-row-as-json-using-new-json-features
Solution
Converting a row to json
It doesn't sound to me like you want to aggregate JSON. You state you want the equivalent of
Aggregating JSON
As a side note, if you do need to aggregate a resultset to json. then the upcoming MySQL 8 will do that for you.
It doesn't sound to me like you want to aggregate JSON. You state you want the equivalent of
row_to_json, if so then I suggest checking out the much simpler JSON_OBJECTSELECT JSON_OBJECT(
'name_field', name_field,
'address_field', address_field,
'contact_age', contact_age
)
FROM contact;Aggregating JSON
As a side note, if you do need to aggregate a resultset to json. then the upcoming MySQL 8 will do that for you.
JSON_ARRAYAGG()Return result set as a single JSON array
JSON_OBJECTAGG()` Return result set as a single JSON object
Code Snippets
SELECT JSON_OBJECT(
'name_field', name_field,
'address_field', address_field,
'contact_age', contact_age
)
FROM contact;Context
StackExchange Database Administrators Q#192208, answer score: 26
Revisions (0)
No revisions yet.