I am trying to search userId for value 22 from nested json in mysql Column
my json is
'{
"data": [
{"calendarId":"11","userId": "12"},
{"calendarId":"21","userId": "22"}
]
}'
I tried the following syntax :
1. where JSON_EXTRACT(column_field,'$.userId') = 22
2. where
JSON_EXTRACT(
column_field,
'$.data[*].userId'
) = 22
- also tried with JSON_Table , but not getting exact nested json value in where condition.
2
Answers
This:
gives:
["12", "22"]
and this:
gives:
Adding a WHERE-clause, to just find the value
22
should not be a problem.P.S. Above is tested using MySQL 8.x, see: DBFIDDLE
Here’s a solution that I tested with MySQL 5.7:
Demo: https://dbfiddle.uk/Hvej9Ucd
If this seems too difficult or complex, then you should consider if JSON is the best way to store your data. This would be far simpler if you stored data in normal rows and columns, and avoided JSON.
This is what it might look like if you stored the data normally: