How to parse output from sse.client in Python? -
i new python , trying ahead around parsing sse client code. using sse client library. code basic , follows sample exactly. here is:
from sseclient import sseclient devid = "xxx" atoken = "xxx" sparkurl = 'https://api.spark.io/v1/devices/' + devid + '/events/?access_token=' + atoken messages = sseclient(sparkurl) msg in messages: print(msg) print(type(msg))
the code runs without problem , see blank lines , sse data coming through. here sample output:
<class 'sseclient.event'> {"data":"0 days, 0:54:43","ttl":"60","published_at":"2015-04-09t22:43:52.084z","coreid":"xxxx"} <class 'sseclient.event'> <class 'sseclient.event'> {"data":"0 days, 0:55:3","ttl":"60","published_at":"2015-04-09t22:44:12.092z","coreid":"xxx"} <class 'sseclient.event'>
the actual output above looks dictionary, type "sseclient.event". trying figure out how parse output can pull out 1 of fields , nothing have tried has worked.
sorry if basic questions, can provide simple guidance on how either convert entire output dictionary or perhaps pull out 1 of fields?
thank in advance!
i figured out. in case else experiences same problem, here how got work. key using msg.data , not msg. converted out using json library , go.
messages = sseclient(sparkurl) msg in messages: outputmsg = msg.data if type(outputmsg) not str: outputjs = json.loads(outputmsg) filtername = "data" #print( filtername, outputjs[filtername] ) print(outputjs[filtername])