Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bytes to json
#4
import json

mesages = [{"id": "1", "txt": "message 1", "start_date": "2014-02-09", "start_time": "16:20", "duration": "01:00"}]
payload = json.dumps(mesages).encode("ascii")
print(payload)
print(type(payload))

# json can also loads bytes
# but here the encoding is not explicit
data = json.loads(payload)

data2 = json.loads(payload.decode("ascii"))

print(data2)
Output:
b'[{"id": "1", "txt": "message 1", "start_date": "2014-02-09", "start_time": "16:20", "duration": "01:00"}]' <class 'bytes'> [{'id': '1', 'txt': 'message 1', 'start_date': '2014-02-09', 'start_time': '16:20', 'duration': '01:00'}]
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
bytes to json - by GrahamL - Dec-04-2020, 09:07 AM
RE: bytes to json - by ndc85430 - Dec-04-2020, 09:29 AM
RE: bytes to json - by GrahamL - Dec-04-2020, 09:53 AM
RE: bytes to json - by DeaD_EyE - Dec-04-2020, 10:39 AM
RE: bytes to json - by GrahamL - Dec-04-2020, 10:44 AM
RE: bytes to json - by GrahamL - Dec-04-2020, 11:22 AM
RE: bytes to json - by buran - Dec-04-2020, 11:46 AM
RE: bytes to json - by GrahamL - Dec-04-2020, 11:59 AM
RE: bytes to json - by buran - Dec-04-2020, 12:13 PM
RE: bytes to json - by GrahamL - Dec-04-2020, 12:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  replace bytes with other byte or bytes BigOldArt 1 10,701 Feb-02-2019, 11:00 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020