Since many people asked this question to me personally, I thought that I was giving this answer a second revision. Here is the gist that has full SQL with SELECT, Migration and View Creation and live sql fiddle (availability is not guaranteed for fiddle).
Let's say you have a table (named: JSON_TABLE) as follows:
ID CITY POPULATION_JSON_DATA ----------------------------------------------------------------------- 1 LONDON {"male" : 2000, "female" : 3000, "other" : 600} 2 NEW YORK {"male" : 4000, "female" : 5000, "other" : 500}
To select all json fields, you can:
SELECT ID, CITY, json_extract(POPULATION_JSON_DATA, '$.male') AS POPL_MALE, json_extract(POPULATION_JSON_DATA, '$.female') AS POPL_FEMALE, json_extract(POPULATION_JSON_DATA, '$.other') AS POPL_OTHER FROM JSON_TABLE;
which leads to:
ID CITY POPL_MALE POPL_FEMALE POPL_OTHER ----------------------------------------------------------------- 1 LONDON 2000 3000 600 2 NEW YORK 4000 5000 500
It can be an expensive operation to run based on your data size and json complexity. I suggest using it for
- Transferring a table to a shared database ( See Appendix 2-B in the text)
- At least create a view (see Appendix 2-C)
Beware: you may have json starting with double quotes (stringified):
"{"male" : 2000, "female" : 3000, "other" : 600}"
Tested with Mysql 5.7 on Ubuntu and Mac OSX Sierra.
tika
source share