I have a sqlite database and in one of the fields I have stored complete json object . I have to make some json select requests . If you see my json the ALL key has value which is an array . We need to extract some data like all comments where "pod" field is fb . How to extract properly when sqlite json has value as an array ?
select json_extract(data,'$."json"') from datatable ; gives me entire thing . Then I do select json_extract(data,'$."json"[0]') but i dont want to do it manually . i want to iterate .
kindly suggest some source where i can study and work on it . MY JSON
{
"ALL": [{
"comments": "your site is awesome",
"pod": "passcode",
"originalDirectory": "case1"
},
{
"comments": "your channel is good",
"data": ["youTube"],
"pod": "library"
},
{
"comments": "you like everything",
"data": ["facebook"],
"pod": "fb"
},
{
"data": ["twitter"],
"pod": "tw",
"ALL": [{
"data": [{
"codeLevel": "3"
}],
"pod": "mo",
"pod2": "p"
}]
}
]
}
create table datatable ( path string , data json1 );
insert into datatable values("1" , json('<abovejson in a single line>'));
Simple List
Where your JSON represents a "simple" list of comments, you want something like:
which, using your sample data, returns:
The use of
json_each()returns a row for every element of the input JSON (datatable.data), starting at the path$.ALL(where$is the top-level, andALLis the name of your array: the path can be omitted if the top-level of the JSON object is required). In your case, this returns one row for each comment entry.The fields of this row are documented at 4.13. The json_each() and json_tree() table-valued functions in the SQLite documentation: the two we're interested in are
key(very roughly, the "row number") andvalue(the JSON for the current element). The latter will contain elements calledcommentandpod, etc..Because we are only interested in elements where
podis equal tofb, we add awhereclause, usingjson_extract()to get atpod(where$.podis relative tovaluereturned by thejson_eachfunction).Nested List
If your JSON contains nested elements (something I didn't notice at first), then you need to use the
json_tree()function instead ofjson_each(). Whereas the latter will only iterate over the immediate children of the node specified,json_tree()will descend recursively through all children from the node specified.To give us some data to work with, I have augmented your test data with an extra element:
If we were to simply switch to using
json_each(), then we see that a simple query (with nowhereclause) will return all elements of the source JSON:Because JSON objects are mixed in with simple values, we can no longer simply add
where json_extract( value, '$.pod' ) = 'fb'because this produces errors whenvaluedoes not represent an object. The simplest way around this is to look at thetypevalues returned byjson_each()/json_tree(): these will be the stringobjectif the row represents a JSON object (see above documentation for other values).Adding this to the
whereclause (and relying on "short-circuit evaluation" to preventjson_extract()being called on non-object rows), we get:which returns:
If desired, we could use
json_extract()to break apart the returned objects:Note: If your structure contained other objects, of different formats, it may not be sufficient to simply select for
type = 'object': you may have to devise a more subtle filtering process.