27

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

1 Answer 1

65

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;
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! That was it. The quotes inside the quotes.
I really wish the Mysql documents would highlight that more. This answer did spare me from going down a rabbit hole.
For compact syntax it seems like you should quote the entire path: fieldnames->"$.field.example-field-1" (with mysql8.0 at least)
Pure luck to have stumbled on this. Worth noting this issue also exist in MySQL 8 but not in MariaDB 10 (at least 10.4 for me, not tested on other versions)

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.