2

I'm struggling with creating a message from a device to the IotHub in the correct format.

I'm using the Azure Client SDK (Microsoft.Azure.Devices.Client)

For better understanding lets start with a small example, we have the following string:

var TableName = "table01";
var PartitionKey = "key01";
string messagePayload = $"{{\"tablename\":\"{TableName}\",\"partitionkey\":\"{PartitionKey}\"}}";

( Taken from the example Send device to cloud telemetry) we create an eventMessage

using var eventMessage = new Microsoft.Azure.Devices.Client.Message(Encoding.UTF8.GetBytes(messagePayload))
{
    ContentEncoding = Encoding.UTF8.ToString(),
    ContentType = "application/json"
};

And then send it to the Cloud:

Console.WriteLine(messagePayload);
await deviceClient.SendEventAsync(eventMessage);

Output from the writeline, which is what I wanted in the first place:

{"tablename":"table01","partitionkey":"key01"}

What I can see in the shell after following the answer about watching incoming IotHub Messages:

{
    "event": {
        "origin": "WinSensorTest",
        "module": "",
        "interface": "",
        "component": "",
        "payload": "{\"tablename\":\"table01\",\"partitionkey\":\"key01\"}"
    }
}

The Problem is, that I want it to either look like the code below or completely without the "event" etc, just the string above.

{
   "event":{
      "origin":"WinSensorTest",
      "module":"",
      "interface":"",
      "component":"",
      "payload":{
         "tablename":"table01",
         "partitionkey":"key01"
      }
   }
}

Where did I go wrong, how can the payload be correct json format?

Edit:

I just tried the same in Java, with the same result. Why does this not work, or is the data seen in the shell not correctly parsed?

chris
  • 181
  • 2
  • 18
  • 1
    There seems to be some interpretation going on here. You clearly have double quotes in the string you encode into bytes, but the payload you observe is missing double quotes around the table name and key. – Lasse V. Karlsen Jul 09 '21 at 07:54
  • @LasseV.Karlsen I apologise, that was an error in the question, as I censored the output a bit and missed those \ – chris Jul 09 '21 at 08:29
  • 1
    I assume what you see in the shell is not parsed at all. The payload may be everything as long as it can be []byte or a stream (of bytes). So it may be a bit of a challenge to format everything to the console in a correct way. But that is just a guess – Sascha Jul 09 '21 at 08:57

1 Answers1

0

If you create a proper Json object first it works and also shows up correct in the shell - interestingly only for this c# project, I tried doing the same in Java on Android and the same wierd formatting stuff still happens even after making an object with gson.

For the solution:

class JsonMessage
{
        public string tablename { get; set; }
        public string partitionkey { get; set; }
}

And then Used JsonMessage and JsonConvert to get the desired payload.

JsonMessage newMsg = new JsonMessage()
    {
        tablename = "table01",
        partitionkey = "key01",
    };

string payload = JsonConvert.SerializeObject(newMsg);


using var eventMessage = new Microsoft.Azure.Devices.Client.Message(Encoding.UTF8.GetBytes(payload))
    {
        ContentEncoding = Encoding.UTF8.ToString(),
        ContentType = "application/json"
    };
chris
  • 181
  • 2
  • 18