Select the first element in a JSON array in Microsoft stream analytics query

半腔热情 提交于 2019-12-23 14:24:13

问题


So I got a bit of a problem. I retrieve some weatherdata from an external API. This is returned as JSON and send to an Azure IoT hub. Stream analytics processes the json into a proper format, but I got a problem here.

The element: Current_Condition, is of an array format. It always has one element on the [0] position. I only need to get the data of that array from that very first position, without a filter for things like id etc.

Under here is the complete data

{
  "deviceId": "aNewDevice",
  "data": {
    "data": {
      "current_condition": [
        {
          "cloudcover": "0",
          "FeelsLikeC": "0",
          "FeelsLikeF": "32",
          "humidity": "100",
          "observation_time": "10:00 AM",
          "precipMM": "0.0",
          "pressure": "1020",
          "temp_C": "2",
          "temp_F": "36",
          "visibility": "0",
          "weatherCode": "143",
          "weatherDesc": [ { "value": "Fog, Mist" } ],
          "weatherIconUrl": [ { "value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0006_mist.png" } ],
          "winddir16Point": "SSW",
          "winddirDegree": "210",
          "windspeedKmph": "7",
          "windspeedMiles": "4"
        }
      ],
      "request": [
        {
          "query": "Nijmegen, Netherlands",
          "type": "City"
        }
      ]
    }
  }
}

Also some explanation on what it is I exactly need to do (not just an example or code answer) would be nice for the future. (The request element hast the same problem after all.)

Thanks in advance :)


回答1:


You need to use GetArrayElement function. For example:

SELECT GetRecordProperty(GetArrayElement(Current_Condition, 0), 'humidity')

To make it a bit nicer you can split query into 2 steps:

WITH CurrentConditions AS
(
    SELECT deviceId, GetArrayElement(Current_Condition, 0) as conditions
    FROM input
)

SELECT deviceID, conditions.humidity
FROM CurrentConditions


来源:https://stackoverflow.com/questions/35503674/select-the-first-element-in-a-json-array-in-microsoft-stream-analytics-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!