1

So let's say I have a row that looks like

id json_data
1  { ... }
2  { ... }
3  { ... }

I want to select the rows that have a key which contains a string abc. I tried to use something like

SELECT * FROM table1 t where JSON_EXTRACT(t.json_data,...

but wasn't able to complete the query because I wasn't sure how to add this condition.

Any help?

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
Dawn17
  • 7,825
  • 16
  • 57
  • 118
  • Does this answer your question? [How to retrieve JSON data from MySQL?](https://stackoverflow.com/questions/15701579/how-to-retrieve-json-data-from-mysql) – WOUNDEDStevenJones Sep 15 '21 at 03:31
  • No because I am trying to check the Key value, not retrieve the value with the key – Dawn17 Sep 15 '21 at 03:34
  • Please edit your question to provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) with some example raw data and what your expected result set should be. You have a lot of `...` in your question, which leaves a lot of room for interpretation. – WOUNDEDStevenJones Sep 15 '21 at 04:02
  • How much overlap will there be with keys/values matching `abc`? You could treat the column as a raw string and search `WHERE t.json_data LIKE '%abc%'`, and possibly manually filter out false positives. – WOUNDEDStevenJones Sep 15 '21 at 04:04
  • Have you tried [JSON_SEARCH()](https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html#function_json-search)? – Bill Karwin Sep 15 '21 at 05:03

1 Answers1

0

If you only need to look at the top level keys, this is simply:

json_keys(json_data) like '%abc%'
ysth
  • 96,171
  • 6
  • 121
  • 214