Home > Enterprise >  Byte Json cannot be loaded after transforming to string json
Byte Json cannot be loaded after transforming to string json

Time:11-10

The Json that its receiving in message is a byte json like so: b'{"_timestamp": 1636472787, "actual": 59.9, "target": 60.0}' The Code is supposed to change the byte Json to String Json and load it to access the items but when I load it I get the following error:

raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Code:

import json
def handle_msg(topic, message):
    m = message.decode("Utf-8")
    print(json.loads(m))

CodePudding user response:

this is happening because you message is null value not as you expected if you write the following it will work for you the following running for me

message = b'{"_timestamp": 1636472787, "actual": 59.9, "target": 60.0}'
topic ="what ever"
import json 
def handle_msg(topic, message):
    m = message.decode("Utf-8")
    print(json.loads(m))
handle_msg(topic, message)
  • Related