Search code examples
azureazure-stream-analytics

Azure Stream Analytics complex query not returning desired results


This is the input JSON packet. I'm writing the transformation query in stream analytics to get source and masterTag properties.

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

Here is what I've:

WITH data AS
(
    SELECT
        source,
        GetArrayElement(data,0) as data_packet 
    FROM input 
)

SELECT 
    source, 
    data_packet.masterTag 
INTO 
    output
FROM data

However, this query only returns one element. How do I update this query to get all elements in the JSON packet?


Solution

  • I did test with your source data and query, it did get 2 output elements. So,I think you mean how to return all elements with your data array has more elements.

    You could use below query:

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

    Json sample:

    [{
          "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",
      }
    ]
    

    Output:

    enter image description here

    Hope it helps you.