Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can mysql json_contains be case insensitive?

Tags:

json

sql

mysql

I want to be able to 'search' case insensitive with json_contains

This is the json example:

[
  {
    "title": "foo Bar",
    "author": "melaine"
  },
  {
    "title": "incredible",
    "author": "steve"
  }
]

What I tried:

SELECT json_contains('[{"title":"foo Bar", "authour": "melaine"}, {"title":"foo barius", "authour": "steve"}]', '{"title":"foo bar"}')

Expected outcome: 1

Real outcome: 0

Becuase I look for "foo bar" and the value in the json is "foo Bar", I do not get a match. Is there a way make this case insensitive so I do get a match?

like image 642
dylanvanw Avatar asked Oct 27 '25 17:10

dylanvanw


1 Answers

You can convert both the JSON (haystack) and the searching block (needle) to lowercase using LOWER() function, for case-insensitive search:

SELECT json_contains(LOWER('[{"title":"foo Bar", "authour": "melaine"}, {"title":"foo barius", "authour": "steve"}]'), 
                     LOWER('{"title":"foo bar"}'))

DB Fiddle Demo

like image 126
Madhur Bhaiya Avatar answered Oct 29 '25 07:10

Madhur Bhaiya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!