'How do I send JSON to event hub from an azure function?

I am trying to process telemetry events from IoT hub, manipulate it a bit by adding/deleting a few JSON properties and send to event hub as described in a previous question. I am using durable entity functions to do the same as detailed in a follow up question.

Overall goal is to store these in a Time Series Insights (TSI) environment and later represent it on some dashboards.

Let us say I calculate temperature in degree Fahrenheit using raw sensor data and put in a JSON message like below.

{
    "Timestamp" : "2021-06-10T00:00:00Z",
    "Parameters" : [
        {
            "Name" : "Temperature",
            "Value" : 89.06
        }
     ]
}

The "Name" and "Value" pair forms a JSON object (sorrounded by {}) and also is an element of JSON array "Parameters". I want to send this message to an event hub and in the event hub I want it to be received as is. So I modified the data type of output binding to binary/stream and setting the result as :

context.set_result(obj) 
#obj is a Python object corresponding to the JSON

Modified Output binding:

    {
      "type": "eventHub",
      "name": "$return",
      "direction": "out",
      "eventHubName": "myeventhub",
      "connection": "conn_eventhub",
      "dataType": "stream"
    }

However when I monitor the messages using Event hub explorer in VS code, I observe that the JSON has been converted to a string and wrapped in a bigger system-defined JSON like below.

{
  "entityExists": true,
  "entityState": "62.00009240670974",
  "results": [
    {
      "isError": false,
      "duration": 4,
      "result": "{\"Timestamp\": \"2021-06-10T00:00:00Z\", \"Parameters\": [{\"Name\": \"Temperature\", \"Value\": 89.06}]}"
    }
  ],
  "signals": []
}

But I don't want the JSON to be sent as a string. Instead I want it to be sent as a JSON object like below which is compatible with Time Series Insights.

{
  "entityExists": true,
  "entityState": "62.00009240670974",
  "results": [
    {
      "isError": false,
      "duration": 4,
      "result": {
          "Timestamp" : "2021-06-10T00:00:00Z",
          "Parameters" : [
              {
                "Name" : "Temperature",
                "Value" : 89.06
              }
          ]
       }
  ],
  "signals": []
}

How can it be done?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source