If anyone would be kind enough to explain the commented out json code in the following, I’d be grateful.
I’ve managed to get the code to work (taken from another forum post click to see original post but only with the json lines commented out.
I think I can decipher the rest of the code but no idea what the json stuff is for/doing here.
When I run the code in idle I get the odd ‘message’ printed, what is this?
from sseclient import SSEClient
messages = SSEClient('https://api.spark.io/v1/events/buttonPressed?access_token=xxxxxxxxxxxxxxxxx')
for msg in messages:
event = str(msg.event).encode('utf-8')
data = str(msg.data).encode('utf-8')
## dataJson = json.loads(data)
You’re the man Rickkas, appreciate all your efforts responding.
It’s tough when you’re on the beginners ladder and this kind of help is so useful.
If I wanted to get the Json side working (no specific need at the moment but keen to learn)…
I’m getting the following when I uncomment the code:
Traceback (most recent call last):
File "/home/pi/Documents/TreatMachine/WorkingRPi/get_publish_message_photon_V3.py", line 14, in <module>
dataJson = json.loads(data)
File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
I’m not positive about this, as I’ve never used the Python SSE client before, but I think you might need to check to make sure data is not an empty string before you call json.loads on it. An empty string is not valid JSON (hence the error) but you may get empty data from keepalive messages. Just guessing after reading this: