I have a sensor which is reporting data into the IoTHub in the following format (via a Modbus server):
15/05/2018 14:56:56> Device: [dev], Data:[[{"DisplayName":"Temperature","HwId":"PI-1","Address":"400002","Value":"192","SourceTimestamp":"2018-05-15 13:56:52"},{"DisplayName":"Humidity","HwId":"PI-1","Address":"400001","Value":"397","SourceTimestamp":"2018-05-15 13:56:52"}]]Properties:
'content-type': 'application/edge-modbus-json'
15/05/2018 14:57:00> Device: [dev], Data:[[{"DisplayName":"Temperature","HwId":"PI-1","Address":"400002","Value":"201","SourceTimestamp":"2018-05-15 13:56:57"},{"DisplayName":"Humidity","HwId":"PI-1","Address":"400001","Value":"397","SourceTimestamp":"2018-05-15 13:56:57"}]]Properties:
'content-type': 'application/edge-modbus-json'
15/05/2018 14:57:06> Device: [dev], Data:[[{"DisplayName":"Temperature","HwId":"PI-1","Address":"400002","Value":"201","SourceTimestamp":"2018-05-15 13:57:02"},{"DisplayName":"Humidity","HwId":"PI-1","Address":"400001","Value":"397","SourceTimestamp":"2018-05-15 13:57:02"}]]Properties:
'content-type': 'application/edge-modbus-json'
15/05/2018 14:57:10> Device: [dev], Data:[[{"DisplayName":"Temperature","HwId":"PI-1","Address":"400002","Value":"195","SourceTimestamp":"2018-05-15 13:57:07"},{"DisplayName":"Humidity","HwId":"PI-1","Address":"400001","Value":"397","SourceTimestamp":"2018-05-15 13:57:07"}]]Properties:
'content-type': 'application/edge-modbus-json'
Each sensor is reported within a separate array entry and also split between the Displayname of the sensor and the Value.
What I want is a JSON Payload that I can feed into PowerBI and so this needs to be in the order:
timestamp:time,humidity:humidity_value,temperature:temperature_value
How do I construct a suitable stream analytics query to do this? This input format is typical of Modbus or OPC-UA type devices so will likely come across this a few times.
I tried to use GetArrayElement/(s) but the array has no name in the JSON so there is nothing to reference.
Per my understanding, you could leverage Azure Stream Analytics JavaScript user-defined functions to flat your data.
Assuming that your data looks like this:
You could create the following UDFs:
GetValueByPropertyName:
GetValueByDisplayName:
Sample query:
TEST: