7

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.

2
  • Paste here the table structure and some real data Commented Sep 14, 2017 at 11:42
  • These are a real data. I don't have table, I want to read the JSON to work with the ids and types and then insert in a secondary table. Commented Sep 14, 2017 at 11:46

1 Answer 1

23
mysql> SET @`json` :=
    -> '[
    '>    {
    '>      "id": 1, "type": 2
    '>    },
    '>    {
    '>      "id": 2, "type": 1
    '>    }
    '> ]';
Query OK, 0 rows affected (0.00 sec)

You can get all ids in an array:

mysql> SELECT JSON_EXTRACT(@`json` ,'$[*].id');
+----------------------------------+
| JSON_EXTRACT(@`json` ,'$[*].id') |
+----------------------------------+
| [1, 2]                           |
+----------------------------------+
1 row in set (0.00 sec)

Can access each JSON id:

mysql> SELECT JSON_EXTRACT(@`json` ,'$[0].id');
+----------------------------------+
| JSON_EXTRACT(@`json` ,'$[0].id') |
+----------------------------------+
| 1                                |
+----------------------------------+
1 row in set (0.00 sec)

Try:

mysql> DROP PROCEDURE IF EXISTS `new_procedure`;
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER //

mysql> CREATE PROCEDURE `new_procedure`(`json` JSON)
    -> BEGIN
    ->   DECLARE `json_items` BIGINT UNSIGNED DEFAULT JSON_LENGTH(`json`);
    ->   DECLARE `_index` BIGINT UNSIGNED DEFAULT 0;
    -> 
    ->   DROP TEMPORARY TABLE IF EXISTS `jsonTemporary`;
    -> 
    ->   CREATE TEMPORARY TABLE IF NOT EXISTS `jsonTemporary`
    ->     (`id` BIGINT UNSIGNED NOT NULL);
    -> 
    ->   WHILE `_index` < `json_items` DO
    ->     INSERT INTO `jsonTemporary` (`id`)
    ->     VALUES (JSON_EXTRACT(`json`, CONCAT('$[', `_index`, '].id')));
    ->     SET `_index` := `_index` + 1;
    ->   END WHILE;
    -> 
    ->   SELECT `id` FROM `jsonTemporary`;
    ->   DROP TEMPORARY TABLE IF EXISTS `jsonTemporary`;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;

mysql> CALL `new_procedure`(@`json`);
+----+
| id |
+----+
|  1 |
|  2 |
+----+
2 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

See db-fiddle.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.