1

IoT Edge v2 with the modbus module sends data to IoT Hub in the format of:

[
{
    "DisplayName": "Voltage",
    "HwId": "",
    "Address": "400001",
    "Value": "200",
    "SourceTimestamp": "2019-01-03 23:40:24"
},
{
    "DisplayName": "Voltage",
    "HwId": "",
    "Address": "400002",
    "Value": "24503",
    "SourceTimestamp": "2019-01-03 23:40:24"
},
...
]

I want to convert this array to rows using a stream analytics query containing the CROSS APPLY GetArrayElements() but this function requires an array name. Obviously there is no name. Any suggestions?

https://learn.microsoft.com/en-us/stream-analytics-query/getarrayelements-azure-stream-analytics https://learn.microsoft.com/en-us/azure/stream-analytics/stream-analytics-parsing-json

KevinH
  • 33
  • 7

2 Answers2

1

Yes, it needs an array name. CROSS APPLY GetArrayElements() is used for nested array.

Example:

[{
      "source": "xda",
      "data": 
        [{
            "masterTag": "UNIFY1",
            "speed": 180
        },
        {
            "masterTag": "UNIFY2",
            "speed": 180
        }],
      "EventEnqueuedUtcTime": "2018-07-20T19:28:18.5230000Z",
  },
  {
      "source": "xda",
      "data": [{
            "masterTag": "UNIFY3",
            "speed": 214
        },
        {
            "masterTag": "UNIFY4",
            "speed": 180
        }],
      "EventEnqueuedUtcTime": "2018-07-20T19:28:20.5550000Z",
  }
]

You could use below sql to convert it to rows:

SELECT 
    jsoninput.source, 
    arrayElement.ArrayValue.masterTag
INTO 
    output
FROM jsoninput
CROSS APPLY GetArrayElements(jsoninput.data) AS arrayElement 

However ,now the input data you provided is a pure array. If you want to convert this array to rows, just use sql:

select jsoninput.* from jsoninput

enter image description here

Jay Gong
  • 23,163
  • 2
  • 27
  • 32
  • Thanks. But the problem is there is no array name and I cannot change that. The data is sent via an external system. – KevinH Jan 04 '19 at 02:46
  • @KevinH So,would you please share what the format of the output you want from you source data?Why you want to use the GetArrayElements method? Please add it into your question? – Jay Gong Jan 04 '19 at 02:52
  • @KevinH hi,any updates now? Does my answer helps you? – Jay Gong Jan 09 '19 at 01:06
0

You don't have to use GetArrayElements. Just selecting json array as input format is enough. Stream analytics reads each object in the array as a record. Same with line or whitespace separated jain objects, each object is read as a record.

Vignesh Chandramohan
  • 1,306
  • 10
  • 15