Query JSON Field

21 views
Skip to first unread message

jacqueli...@gmail.com

unread,
Apr 23, 2018, 9:18:02 PM4/23/18
to web2py-users
Hello, 

I'm wondering if there is a way to query a JSON object once its been inserted into a SQLite table? 
I have a Program table with a column : 

Field('performance_json', 
                      type='json')

and I'm trying to query based on this JSON's data within a simple JSON object
{"name": name, "age": 25} 

Is there a clean way to query the JSON object? 
I've tried using the DAL's function's


Anthony

unread,
Apr 23, 2018, 9:38:46 PM4/23/18
to web2py-users
In SQLite, probably the best you can do is:

db(db.mytable.performance_json.contains('"name": "%s"' % name) &
   db
.mytable.performance_json.contains('"age": 25')).select()

In databases that do not support JSON natively (such as SQLite), the JSON is simply stored as a string, so you can use the usual text search methods to match the relevant fragments of JSON.

Anthony
Reply all
Reply to author
Forward
0 new messages