Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL JSON_EXTRACT path expression error

Tags:

json

mysql

The syntax looks right to me, any help would be appreciated!

mysql> select fieldnames from tablename limit 5;
+--------------------------------------------------------+
| fieldnames                                             |
+--------------------------------------------------------+
| {"example-field-1": "val2"}                            |
| {"example-field-2": "val1"}                            |
| {"example-field-1": "val1", "example-field-3": "val1"} |
| {"example-field-2": "val1"}                            |
| {"example-field-2": "val2"}                            |
+--------------------------------------------------------+
mysql> select JSON_EXTRACT(fieldnames, '$.example-field-1') from tablename;
ERROR 3143 (42000): Invalid JSON path expression. The error is around character position 17 in '$.example-field-1'.

MySQL 5.7.10

like image 547
2achary Avatar asked Mar 01 '16 23:03

2achary


1 Answers

Can you try this advice from https://dev.mysql.com/doc/refman/5.7/en/json.html

As mentioned previously, path components that name keys must be quoted if the unquoted key name is not legal in path expressions. Let $ refer to this value.

select JSON_EXTRACT(fieldnames, '$."example-field-1"') from tablename;

NOTE

If you have more fields, you must quote each key not the whole path:

select JSON_EXTRACT(fieldnames, '$."field"."example-field-1"') from tablename;
like image 160
alexander.polomodov Avatar answered Nov 01 '22 04:11

alexander.polomodov