1

I have the following byte variable, which I am trying to parse to become JSON.

payload = b'{\n  "time" : "2021-03-09T02:14:13.312Z",\n    "age" : 56,\n    "trackingMode" : "PERMANENT_TRACKING",\n    "number" : 4.0,}'

payload_decoded = codecs.decode(payload, 'utf-8')
json_record = json.load(payload_decoded, parse_float=Decimal)
print(json_record)

It's returning this error.. How can I parse my byte var above to Json? Thank You!

[ERROR] AttributeError: 'str' object has no attribute 'read'

beginner-fella
  • 135
  • 2
  • 11

1 Answers1

2

You need

json_record = json.loads(payload_decoded, parse_float=Decimal)

Explanation: json.load expects a file handle that contains JSON, whereas json.loads expects a string or bytes.

foglerit
  • 7,792
  • 8
  • 44
  • 64