0

I am sending a JSON file from the cloud to a device using Azure IoT cloud-to-device messages via the Python SDKs.

The file contains lots of new lines and tabs which I would like to preserve. The file received of course must be in the exact same format as the one being sent.

This is on the sending end (cloud) :

FILENAME = "my_file.json"
f = open (FILENAME, "r")
data = f.read()
registry_manager.send_c2d_message(DEVICE_ID, data)

And on the receiving end (device) :

message = client.receive_message()
received_file = open("output.json", "w")
received_file.write(str(message))
received_file.close()

However the file contains just one line with the special characters b' \n \t , and not the actual tabs and new lines etc. Here is just the beginning of it :

b'{\n    "group1":\n    [\n        {\n    

How should I get this to format properly and not print the special characters, but instead lines and tabs etc.? Thanks in advance.

Engineer999
  • 3,683
  • 6
  • 33
  • 71
  • The message needs to be _decoded_: `received_file.write(message.decode('utf-8'))` (substitute the correct encoding if utf-8 is not what's being sent). – snakecharmerb Jul 20 '20 at 18:28
  • @snakecharmerb Thanks for your reply. However " AttributeError: 'Message' object has no attribute 'decode'" I am using Python3 – Engineer999 Jul 21 '20 at 09:03

0 Answers0