I'm trying to extract an array from a JSON object with MYSQL
SELECT json_extract(jsonObjectValue,'$[*].name') as array FROM `TEST` WHERE name='jsonObject'
The query above has this as a result
...
["elem1", "elem1", "elem2"]
["elem5", "elem1", "elem2", "elem4"]
...
I tried doing to extract the array by doing this:
SELECT json_extract(json_extract(jsonObjectValue,'$[*].name'),'$[*]') as array FROM `TEST` WHERE name='jsonObject'
The desired result would look like this:
...
"elem1"
"elem1"
"elem2"
"elem5"
"elem1"
"elem2"
"elem4"
...
but the actual result is:
...
["elem1", "elem1", "elem2"]
["elem5", "elem1", "elem2", "elem4"]
...
I also tried to change '$[*]' inside the JSON extract to '$[0]' it only shows the first element of the array.
Update
to reproduce the issue run these queries:
CREATE TABLE `TEST` (
`jsonObjectValue` varchar(1000) NOT NULL,
`name` varchar(1000) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
and for the data run:
INSERT INTO `TEST` (`jsonObjectValue`, `name`) VALUES
('[{\"name\":\"elem1\"},{\"name\":\"elem1\"},{\"name\":\"elem2\"}]',
'JsonObject'),
('test', 'name'),
('test2', 'test2'),
('[{\"name\":\"elem5\"},{\"name\":\"elem1\"},{\"name\":\"elem2\"},
{\"name\":\"elem4\"}]', 'jsonObject');
Any help would be appreciated.
MySQL 5$[x].nameas path where x comes from a table of numbers 0...999 might work.