Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL json_search on numeric values

Tags:

json

sql

mysql

I've got a json list of objects like that

[{
    "something": "bla",
    "id": 2
}, {
    "something": "yes",
    "id": 1
}]

My id field is always a numeric value. But when I try to find id = 2, MySQL returns NULL

select
    json_search(
        json_extract(
            '[{"something": "bla" ,"id": 2}, {"something": "yes","id": 1}]',
            "$[*].id"
        ),
        'one',
        2
    ) as json_search;

json_search |
------------|
            |

When I use a string as value in my json id object instead of a numeric value, I got a result with Index 0.

select
    json_search(
        json_extract(
            '[{"something": "bla" ,"id": "2"}, {"something": "yes","id": 1}]',
            "$[*].id"
        ),
        'one',
        "2"
    ) as json_search;

json_search |
------------|
"$[0]"      |

I'm using MySQL 5.7.17

@@version  |
-----------|
5.7.17-log |

Is numeric search in json arrays not provided in MySQL?

like image 952
Markus Avatar asked Jun 29 '17 13:06

Markus


People also ask

What is JSON extract () function in MySQL?

We can use the JSON_EXTRACT function to extract data from a JSON field. The basic syntax is: JSON_EXTRACT(json_doc, path) For a JSON array, the path is specified with $[index] , where the index starts from 0: mysql> SELECT JSON_EXTRACT('[10, 20, 30, 40]', '$[0]'); +------------------------------------------+

How do I query a JSON column in MySQL?

MySQL provides two operators ( -> and ->> ) to extract data from JSON columns. ->> will get the string value while -> will fetch value without quotes. As you can see ->> returns output as quoted strings, while -> returns values as they are. You can also use these operators in WHERE clause as shown below.

Can I store JSON in MySQL?

MySQL supports a native JSON data type defined by RFC 7159 that enables efficient access to data in JSON (JavaScript Object Notation) documents. The JSON data type provides these advantages over storing JSON-format strings in a string column: Automatic validation of JSON documents stored in JSON columns.

Does MySQL 5.7 support JSON data type?

MySQL version 5.7. 8 introduces a JSON data type that allows you to access data in JSON documents.


1 Answers

You can try something complicated, not intuitive and possibly with performance problems, but it's an option:

mysql> SELECT JSON_SEARCH(
    ->   REPLACE(
    ->     REPLACE(
    ->       REPLACE(
    ->         JSON_EXTRACT('[
    '>                         {"something": "bla" ,"id": 2},
    '>                         {"something": "yes","id": 1}
    '>                       ]', "$[*].id"),
    ->       ', ', '","'),
    ->     '[', '["'),
    ->   ']', '"]'),
    -> 'one', '2') `json_search`;
+-------------+
| json_search |
+-------------+
| "$[0]"      |
+-------------+
1 row in set (0.00 sec)
like image 110
wchiquito Avatar answered Oct 03 '22 03:10

wchiquito