Dennis
Dennis

Reputation: 8111

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

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

Upvotes: 6

Views: 5487

Answers (1)

Shubham Bansal
Shubham Bansal

Reputation: 388

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"');

Upvotes: 10

Related Questions