iothub-message-schema: twinChangeNotification

Chandra Mohan 461 Reputation points
2020-06-05T03:16:31.613+00:00

Hi

I would like to know if there is a way to de-serialize data from the Azure function that is triggered when the reported property is updated in the IoT Hub Device Twin? Is there any schema or predefined class in EventData SDK that can be mapped to the EventData.Body?

// example code.
       [FunctionName("IotMessage")]
            public async void Run([IoTHubTrigger("messages/events", Connection = "iotConnection", ConsumerGroup = "functionapp")]EventData message, ILogger log)
      {
    // iothub-message-schema: twinChangeNotification?
      var JsonString1 = Encoding.UTF8.GetString(message.Body.Array);

    }
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,131 questions
{count} vote

Accepted answer
  1. QuantumCache 20,031 Reputation points
    2020-06-05T08:38:33.583+00:00

    Hello @Chandra Mohan ,

    There are many ways to de-serialize the dynamic Json object, below is one among them, please give a try and check if it works for you.

    dynamic stuff = JsonConvert.DeserializeObject(JsonString1);

       [FunctionName("Function1")]  
            public static void Run([IoTHubTrigger("messages/events", Connection = "ConnectionString", ConsumerGroup = "funcgroup")]EventData message, ILogger log)  
            {  
                log.LogInformation($"C# IoT Hub trigger function processed a message: {Encoding.UTF8.GetString(message.Body.Array)}");  
      
                var JsonString1 = Encoding.UTF8.GetString(message.Body.Array);  
      
                dynamic stuff = JsonConvert.DeserializeObject(JsonString1);  
      
            }  
    

    9120-3.png

    Please visit the Azure IoT Samples-C-sharp for more info

    9149-4.png

    9127-5.png


0 additional answers

Sort by: Most helpful