简体   繁体   中英

Problems using json_extract in Sqlite for key with colon (:) in it

I have an example data set like below

id|accountid|attributes|created|type

1|10|{'base:instances': '{}', 'cont:contact': 'CLOSED', 'cont:contactchanged': '1468516440931', 'devconn:lastchange': '1462387904432', 'devconn:signal': '100', 'devconn:state': 'ONLINE', 'devpow:backupbatterycapable': 'false', 'devpow:battery': '66', 'devpow:linecapable': 'false', 'devpow:source': 'BATTERY', 'devpow:sourcechanged': '1462387904403', 'temp:temperature': '25.75'}|2016-05-04 18:51:44+0000|Test

2|20|{'base:instances': '{}', 'cont:contact': 'CLOSED', 'cont:contactchanged': '1468516440931', 'devconn:lastchange': '1462387904432', 'devconn:signal': '100', 'devconn:state': 'ONLINE', 'devpow:backupbatterycapable': 'false', 'devpow:battery': '66', 'devpow:linecapable': 'false', 'devpow:source': 'BATTERY', 'devpow:sourcechanged': '1462387904403', 'temp:temperature': '25.75'}|2016-05-04 18:51:44+0000|Prod

3|30|{'base:instances': '{}', 'cont:contact': 'CLOSED', 'cont:contactchanged': '1468516440931', 'devconn:lastchange': '1462387904432', 'devconn:signal': '100', 'devconn:state': 'ONLINE', 'devpow:backupbatterycapable': 'false', 'devpow:battery': '66', 'devpow:linecapable': 'false', 'devpow:source': 'BATTERY', 'devpow:sourcechanged': '1462387904403', 'temp:temperature': '25.75'}|2016-05-04 18:51:44+0000|Prod

4|40|{'base:instances': '{}', 'cont:contact': 'CLOSED', 'cont:contactchanged': '1468516440931', 'devconn:lastchange': '1462387904432', 'devconn:signal': '100', 'devconn:state': 'ONLINE', 'devpow:backupbatterycapable': 'false', 'devpow:battery': '66', 'devpow:linecapable': 'false', 'devpow:source': 'BATTERY', 'devpow:sourcechanged': '1462387904403', 'temp:temperature': '25.75'}|2016-05-04 18:51:44+0000|Test

I import this to sqlite3 3.13 to do some analysis (.mode csv, .headers on, .separator '|', .import file.csv dev)

As you can see the second field is json formatted data the keys all have : in the names and I think part of my issue.

I would like to and select all rows with column type matching Test and print out the devpow:battery value from the json in attributes column

I have tried all the below and I can't get this to work

select json_extract(dev.attributes, '$.devpower:battery') from dev where type=="Test";

select attributes.[devpower:battery] from dev where type=="Test";

select 'attributes.devpower:battery' from dev where type=="Test";

And quite a few permeations of the above. Any help is greatly appreciated.

devpow:battery is a perfectly valid object label, and it works if you are actually using valid JSON (the values in your example are not), and if you spell the label correctly (which you did not):

> SELECT attributes FROM dev;
{"devpow:battery": "66"}
> SELECT json_extract(dev.attributes, '$.devpow:battery') FROM dev WHERE ...;
66

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM