Ibrahim Lawal
Ibrahim Lawal

Reputation: 1228

How to extract values from a numeric-keyed nested JSON field in MySQL

I have a MySQL table with a JSON column called sent. The entries in the column have information like below:

{
 "data": {
  "12":"1920293"
 }
}

I'm trying to use the mysql query:

select sent->"$.data.12" from mytable

but I get an exception:

Invalid JSON path expression. The error is around character position 9.

Any idea How I can extract the information? The query works fine for non-numeric subfields.

Upvotes: 18

Views: 5353

Answers (1)

Jeffrey Kilelo
Jeffrey Kilelo

Reputation: 6473

@Ibrahim, You have an error in your code. If you use number (or spaced words) as key in a JSON data type in MySQL, you'll need to double-quote it.

Therefore, the correct MySQL statement in your case is:

select sent->'$.data."12"' FROM mytable;

Thanks, @JeffreyKilelo

Upvotes: 23

Related Questions