Select Microsoft 流分析查询中 JSON 数组中的第一个元素
Select the first element in a JSON array in Microsoft stream analytics query
所以我遇到了一点问题。
我从外部 API 检索了一些天气数据。这将作为 JSON 返回并发送到 Azure IoT 中心。流分析将 json 处理成正确的格式,但我在这里遇到了问题。
元素:Current_Condition,为数组格式。它总是在 [0] 位置有一个元素。我只需要从第一个位置获取该数组的数据,而不需要对 id 等内容进行筛选。
下面是完整的数据
{
"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"
}
]
}
}
}
还有一些关于我究竟需要做什么的解释(不仅仅是示例或代码答案)对未来会有好处。 (毕竟request元素也有同样的问题。)
提前致谢:)
您需要使用GetArrayElement功能。例如:
SELECT GetRecordProperty(GetArrayElement(Current_Condition, 0), 'humidity')
为了让它更好一点,您可以将查询分为 2 个步骤:
WITH CurrentConditions AS
(
SELECT deviceId, GetArrayElement(Current_Condition, 0) as conditions
FROM input
)
SELECT deviceID, conditions.humidity
FROM CurrentConditions
所以我遇到了一点问题。 我从外部 API 检索了一些天气数据。这将作为 JSON 返回并发送到 Azure IoT 中心。流分析将 json 处理成正确的格式,但我在这里遇到了问题。
元素:Current_Condition,为数组格式。它总是在 [0] 位置有一个元素。我只需要从第一个位置获取该数组的数据,而不需要对 id 等内容进行筛选。
下面是完整的数据
{
"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"
}
]
}
}
}
还有一些关于我究竟需要做什么的解释(不仅仅是示例或代码答案)对未来会有好处。 (毕竟request元素也有同样的问题。)
提前致谢:)
您需要使用GetArrayElement功能。例如:
SELECT GetRecordProperty(GetArrayElement(Current_Condition, 0), 'humidity')
为了让它更好一点,您可以将查询分为 2 个步骤:
WITH CurrentConditions AS
(
SELECT deviceId, GetArrayElement(Current_Condition, 0) as conditions
FROM input
)
SELECT deviceID, conditions.humidity
FROM CurrentConditions