简体   繁体   中英

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

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 :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM