search

Home  >  Q&A  >  body text

Find matching integers in JSON.

<p>I want to find 123 in a JSON string in MySQL. </p><p>This is my query:</p><p><br /></p> <pre class="brush:php;toolbar:false;">SELECT * FROM `reports` WHERE JSON_CONTAINS(json_data, 123, "$.respCode");</pre> <p>When I run this code I get the following error: </p> <blockquote> <pre class="brush:php;toolbar:false;">Error Code: 3146. Invalid data type for JSON data in argument 1 to function json_contains; a JSON string or JSON type is required. 0.020 sec</pre> </blockquote> <p>My json looks like this in the json_data column: </p> <pre class="lang-json prettyprint-override"><code>{ "bla":"", "bla2":"", "bla3":"", "bla4":"", "respCode ":"123", "bla5":"" } </code></pre> <p>I also tried this</p> <p><code>WHERE JSON_UNQUOTE(JSON_EXTRACT('json_data', "$.respCode")) = '123'</code></p>
P粉252116587P粉252116587486 days ago484

reply all(1)I'll reply

  • P粉617597173

    P粉6175971732023-07-26 00:54:30

    This is the syntax of the MySQL JSON_CONTAINS() function:

    JSON_CONTAINS(target_json, candidate_json)
    JSON_CONTAINS(target_json, candidate_json, path)

    You can try

    SELECT * FROM `reports` 
    WHERE JSON_CONTAINS(json_data, '{"respCode":"123"}');

    DB Fiddle Demo

    reply
    0
  • Cancelreply