Subscribe to spark events using python

This is an old thread but thought I’d add my code in case someone else runs into the same issue. It took me a few hours to figure out. Maybe someone will find it useful and save them time.

#prior code omitted from this post cleanliness
for msg in messages:
    
    #Get the message event type. This is the Publish Event name. 
    event = str(msg.event)

    #Only add events to SQL that have the event name of "Stat"
    if event == 'Stat':

        #use str and JSON.Loads to decifer string/JSON object from Particle into a Python Dictionary
        data = str(msg.data)
        msgDict = json.loads(data)
        dataDict = json.loads(msgDict["data"])
        msgDict.update({'data': dataDict})

        #Now that the message from Particle is a Pyhton Dictionary, access info within it like this:
        DEVICE_ID=msgDict["coreid"], 
        TempF=msgDict['data']['TempF'] 

        #Do whatever you want with the data now.