Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use MySQL's JSON_EXTRACT with integer keys in JSON string?

Tags:

json

sql

mysql

Is it possible to use JSON_EXTRACT with integer keys?

I want to extract [273, 140] from below, but the SQL does not work ...

SELECT json_extract('{"1": [273, 140], "2": [273.5, 198.5], "3": [209, 191]}', '$.1');

I am trying to use syntax from https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html#operator_json-column-path

I get error:

ERROR 3143 (42000): Invalid JSON path expression. 
The error is around character position 3
like image 506
Dennis Avatar asked Jan 29 '23 19:01

Dennis


1 Answers

You should use double quotes around the key if it is a number. This works:

select json_extract('{"1": [273, 140], "2": [273.5, 198.5], "3": [209, 191]}', '$."1"');
like image 146
Shubham Bansal Avatar answered Jan 31 '23 08:01

Shubham Bansal