Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search value from json in sqlite - Android

I have table named User contain column named userDetails. userDetails column having data in json format like below.

{
    "id": "77",
    "uid": "247",
    "email": "[email protected]",
}

How to query like 'where uid = 247' which is present in this json object.

like image 913
Poyyamozhi Ramakrishnan Avatar asked Oct 24 '25 14:10

Poyyamozhi Ramakrishnan


1 Answers

If the column userDetails is a valid JSON string, you can use the function json_extract():

select * 
from user
where json_extract(userDetails, '$.uid') = '247' 

If you can't use SQLite's JSON1 extension, you can do it by treating userDetails as a normal string and use the operator LIKE:

select * 
from user
where ',' || replace(replace(replace(userDetails, '{', ''), '}', ''), ' ', '') || ',' like '%,"uid":"' || '247' ||'",%'

Replace the ? placeholder with the id you search for, like '247'.

See a simplified demo.

like image 95
forpas Avatar answered Oct 27 '25 04:10

forpas