Read JSON array in MYSQL

Viewed 29325

I'm unable to extract determinate data in a JSON.

I have this JSON:

[{"id":1, "type":2}, {"id":2, "type":1}]

I want to recover all the ids in a variable in my stored procedure.

CREATE DEFINER=`root`@`%` PROCEDURE `new_procedure`(a JSON)
BEGIN
   SELECT JSON_TYPE(a);
   -- return ARRAY

   SELECT a,JSON_EXTRACT(a,'$.id');
END

a return the JSON, but JSON_EXTRACT is empty

Even I prove to save the JSON in a temporary table

CREATE DEFINER=`root`@`%` PROCEDURE `new_procedure`(a JSON)
BEGIN

   SELECT JSON_TYPE(a);
   DROP TEMPORARY TABLE IF EXISTS jsonTemporary;

   CREATE TEMPORARY TABLE jsonTemporary SELECT a;

   SELECT *,a->'$.id',a->>'$.id',JSON_EXTRACT(a,'$.id') FROM jsonTemporary;
END

But the result is the same only the first column return something.

1 Answers
Related