UPDATE: this is now possible in MySQL 8 via the JSON_TABLE function: https://dev.mysql.com/doc/refman/8.0/en/json-table-functions.html
I like the new JSON functions in MySQL 5.7, but I come across a block trying to combine values ββfrom JSON into a regular table structure.
Grab JSON, manipulate and extract arrays from it, etc. Very simple. JSON_EXTRACT completely. But what about backtracking from a JSON array to strings? Maybe I understand the existing MySQL JSON functionality, but I could not figure it out.
For example, let's say I have a JSON array and I want to insert a row for each element in the array with its own value? The only way I found is to write the group JSON_EXTRACT (... '$ [0]') JSON_EXTRACT (... '$ [1]'), etc. And combine them together.
Or, say, I have a JSON array and I want GROUP_CONCAT () to represent it on a single line, separated by commas?
In other words, I know I can do this:
SET @j = '[1, 2, 3]'; SELECT GROUP_CONCAT(JSON_EXTRACT(@j, CONCAT('$[', xn, ']'))) AS val FROM ( SELECT 0 AS n UNION SELECT 1 AS n UNION SELECT 2 AS n UNION SELECT 3 AS n UNION SELECT 4 AS n UNION SELECT 5 AS n ) x WHERE xn < JSON_LENGTH(@j);
But it hurts my eyes. And my heart.
How can I do something like:
SET @j = '[1, 2, 3]'; SELECT GROUP_CONCAT(JSON_EXTRACT(@j, '$[ * ]'))
... and does it combine the values ββin the array with the JSON array itself?
I think I'm looking here for something like JSON_SPLIT according to:
SET @j = '[1, 2, 3]'; SELECT GROUP_CONCAT(val) FROM JSON_SPLIT(JSON_EXTRACT(@j, '$[ * ]'), '$')
If MySQL had the correct table return function STRING_SPLIT (val, 'separator'), I could hack it (avoiding being damned), but it is also not available.