How to select values from JSON in mysql

2020-02-06 23:40发布

问题:

Hi can anyone tell me what is wrong with this query.

DECLARE @json LONGTEXT;

SET @json = '[ { "name":"John Smith",  "address":"780 Mission St, San Francisco, CA 94103"}, { "name":"Sally Brown",  "address":"75 37th Ave S, St Cloud, MN 94103"}, { "name":"John Johnson",  "address":"1262 Roosevelt Trail, Raymond, ME 04071"}     ]';

##SELECT @json;

SELECT * FROM JSON_TABLE (@json, '$[*]' COLUMNS (
                name VARCHAR(40)  PATH '$.name',
                address VARCHAR(100) PATH '$.address'));

Error that i get is

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(@json, '$[*]' COLUMNS ( name VARCHAR(40) PATH '$.name', ' at line 1

Just for Information I am using TOAD as tool to connect to my cloud my sql instance.

回答1:

First, in mysql you don't need to declare a variable, in this case. Just use 'SET' keyword. And finaly, you need put an alias for your 'select' query. Like this:

SET @json = '[ { "name":"John Smith",  "address":"780 Mission St, San Francisco, CA 94103"}, { "name":"Sally Brown",  "address":"75 37th Ave S, St Cloud, MN 94103"}, { "name":"John Johnson",  "address":"1262 Roosevelt Trail, Raymond, ME 04071"}     ]';

##SELECT @json;

SELECT * FROM JSON_TABLE (@json, '$[*]' COLUMNS (
                `name` VARCHAR(40)  PATH '$.name',
                `address` VARCHAR(100) PATH '$.address')) AS T;


回答2:

Ideally you should be upgrading to MYSQL 8, this answer is more meant to have fun

You need to write tricky SQL to simulate/emulate MySQL's 8.0 JSON_TABLE() in versions below MySQL 8

Query

SET @json = '[ { "name":"John Smith",  "address":"780 Mission St, San Francisco, CA 94103"}, { "name":"Sally Brown",  "address":"75 37th Ave S, St Cloud, MN 94103"}, { "name":"John Johnson",  "address":"1262 Roosevelt Trail, Raymond, ME 04071"}     ]';

##SELECT @json;

SELECT * FROM JSON_TABLE (@json, '$[*]' COLUMNS (
                `name` VARCHAR(40)  PATH '$.name',
                `address` VARCHAR(100) PATH '$.address')) AS T;

Result

| name         | address                                 |
| ------------ | --------------------------------------- |
| John Smith   | 780 Mission St, San Francisco, CA 94103 |
| Sally Brown  | 75 37th Ave S, St Cloud, MN 94103       |
| John Johnson | 1262 Roosevelt Trail, Raymond, ME 04071 |

see demo

MySQL's 5.7 simulate/emulate query involves using a number generator and multiple native MySQL's JSON functions.

Query

SELECT 
   REPLACE(JSON_EXTRACT(json_record.json, CONCAT('$[',number,'].name')), '"', '') AS name
 , REPLACE(JSON_EXTRACT(json_record.json, CONCAT('$[',number,'].address')), '"', '') AS address
FROM (
   SELECT 
     @row := @row + 1 AS number
   FROM (
      SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION   SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
      ) row1
      CROSS JOIN (
      SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION  SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
    ) row2
    CROSS JOIN (
      SELECT @row := -1 
    ) init_user_params 
  ) AS number_generator
CROSS JOIN (
SELECT 
   JSON_LENGTH(json_information.json) - 1 AS json_length
 , json_information.json
FROM (
  SELECT 
    record.json
  FROM (
    SELECT 
      '
      [{
            "name": "John Smith",  
            "address": "780 Mission St, San Francisco, CA 94103"
      }, {
           "name": "Sally Brown",
           "address": "75 37th Ave S, St Cloud, MN 94103"
      }, {
           "name": "John Johnson",
           "address": "1262 Roosevelt Trail, Raymond, ME 04071"
      }]
     ' AS json
    FROM 
     DUAL   
  ) AS record  
) AS json_information

) AS json_record
WHERE 
 number BETWEEN 0 AND json_length           

Result

| name         | address                                 |
| ------------ | --------------------------------------- |
| John Smith   | 780 Mission St, San Francisco, CA 94103 |
| Sally Brown  | 75 37th Ave S, St Cloud, MN 94103       |
| John Johnson | 1262 Roosevelt Trail, Raymond, ME 04071 |

see demo