[英]How to check json and do filtering in Logic App
在我的邏輯應用程序中,其中一個操作項給我 Json 值,如下所示。 這記錄在撰寫動作中。
{
"device1": 24,
"device2": 25,
"device3": 26
}
我只想獲取值等於和大於 25 的設備名稱和值(在本例中為 device2 和 device3),然后在后續方法中傳遞該值,例如為每個值為 25 的設備名稱創建和發送警報消息或者更多。
我怎樣才能在邏輯應用程序中做到這一點?
這是對我有用的解決方法之一。 為了將 Json 過濾為定義明確、簡單的格式(即值大於 25 的設備),也為了將來使用,我們需要將 json 的格式轉換為
{
"device1": 24,
"device2": 25,
"device3": 26
}
到
[
{
"deviceName": "\"device1\"",
"value": "24"
},
{
"deviceName": "\"device3\"",
"value": "26"
},
{
"deviceName": "\"device2\"",
"value": "25"
}
]
我們首先需要將 Json 轉換為數組。 這可以通過兩種方式完成Way -1 (using subString())
方法 1 步驟 1 中的語法
substring(string(outputs('Compose')),1,sub(lastIndexOf(string(outputs('Compose')),'}'),1))
方法 1 步驟 2 中的語法
array(split(string(outputs('Convert_To_Array_Method_1_step_1')),','))
方式-2(使用replace())
方法 2 步驟 1 中的語法
replace(string(outputs('Compose')),'{','[')
方法 2 步驟 2 中的語法
replace(string(outputs('Convert_To_Array_Method_2_step_1')),'}',']')
output:
您需要初始化一個數組變量以存儲結果 json。我們現在可以通過采用syntax in Method 1 step 2
或syntax in Method 2 step 2
輸出中的語法提取 json 中的值 For each connector。
在Extract values
compose connector 中,我試圖通過采用 substring 表達式來提取設備的值。
提取值中的語法
substring(item(),add(indexOf(item(),':'),1),sub(length(item()),add(indexOf(item(),':'),1)))
在這里,我只是提取“:”之后的值。
設備名稱也是如此。 我正在提取從索引 0 到“:”的設備名稱。
格式 JSON 中的語法
{ "inputs": { "deviceName": "@substring(item(), 0, indexOf(item(), ':'))", "value": "@outputs('Extract_Value')" } }
最后,我將結果 Formatted Json 存儲到一個變量中。
output:
現在我只是解析 Formatted 變量,它給我device name
和value
的結果。
在下一步中,我使用條件連接器檢查值是否大於或等於 25
條件語法
int(items('For_each_2')['value'])
如果條件為真,則它將值存儲到Required Values
。
output:
這是我的邏輯應用程序的代碼視圖
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Compose": {
"inputs": {
"device1": 24,
"device2": 25,
"device3": 26
},
"runAfter": {},
"type": "Compose"
},
"Convert_To_Array_Method_1_step_1": {
"inputs": "@substring(string(outputs('Compose')),1,sub(lastIndexOf(string(outputs('Compose')),'}'),1))",
"runAfter": {
"Compose": [
"Succeeded"
]
},
"type": "Compose"
},
"Convert_To_Array_Method_1_step_2": {
"inputs": "@array(split(string(outputs('Convert_To_Array_Method_1_step_1')),','))",
"runAfter": {
"Convert_To_Array_Method_1_step_1": [
"Succeeded"
]
},
"type": "Compose"
},
"Convert_To_Array_Method_2_step_1": {
"inputs": "@replace(string(outputs('Compose')),'{','[')",
"runAfter": {
"Convert_To_Array_Method_1_step_2": [
"Succeeded"
]
},
"type": "Compose"
},
"Convert_To_Array_Method_2_step_2": {
"inputs": "@replace(string(outputs('Convert_To_Array_Method_2_step_1')),'}',']')",
"runAfter": {
"Convert_To_Array_Method_2_step_1": [
"Succeeded"
]
},
"type": "Compose"
},
"Final_Formated_JSON": {
"inputs": "@variables('formatedArray')",
"runAfter": {
"For_each": [
"Succeeded"
]
},
"type": "Compose"
},
"Final_Values": {
"inputs": "@variables('Required Values')",
"runAfter": {
"For_each_2": [
"Succeeded"
]
},
"type": "Compose"
},
"For_each": {
"actions": {
"Append_to_array_variable": {
"inputs": {
"name": "formatedArray",
"value": "@outputs('Formated_JSON')"
},
"runAfter": {
"Formated_JSON": [
"Succeeded"
]
},
"type": "AppendToArrayVariable"
},
"Extract_Value": {
"inputs": "@substring(item(),add(indexOf(item(),':'),1),sub(length(item()),add(indexOf(item(),':'),1)))",
"runAfter": {},
"type": "Compose"
},
"Formated_JSON": {
"inputs": {
"deviceName": "@substring(item(), 0, indexOf(item(), ':'))",
"value": "@outputs('Extract_Value')"
},
"runAfter": {
"Extract_Value": [
"Succeeded"
]
},
"type": "Compose"
}
},
"foreach": "@outputs('Convert_To_Array_Method_1_step_2')",
"runAfter": {
"Initialize_variable_to_store_the_formatted_Json_": [
"Succeeded"
]
},
"type": "Foreach"
},
"For_each_2": {
"actions": {
"Condition": {
"actions": {
"Append_to_array_variable_2": {
"inputs": {
"name": "Required Values",
"value": "@items('For_each_2')"
},
"runAfter": {},
"type": "AppendToArrayVariable"
}
},
"expression": {
"and": [
{
"greaterOrEquals": [
"@int(items('For_each_2')['value'])",
25
]
}
]
},
"runAfter": {},
"type": "If"
}
},
"foreach": "@body('Parse_JSON')",
"runAfter": {
"Initialize_variable_to_store_required_values": [
"Succeeded"
]
},
"type": "Foreach"
},
"Initialize_variable_to_store_required_values": {
"inputs": {
"variables": [
{
"name": "Required Values",
"type": "array"
}
]
},
"runAfter": {
"Parse_JSON": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"Initialize_variable_to_store_the_formatted_Json_": {
"inputs": {
"variables": [
{
"name": "formatedArray",
"type": "array"
}
]
},
"runAfter": {
"Convert_To_Array_Method_2_step_2": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"Parse_JSON": {
"inputs": {
"content": "@variables('formatedArray')",
"schema": {
"items": {
"properties": {
"deviceName": {
"type": "string"
},
"value": {
"type": "string"
}
},
"required": [
"deviceName",
"value"
],
"type": "object"
},
"type": "array"
}
},
"runAfter": {
"Final_Formated_JSON": [
"Succeeded"
]
},
"type": "ParseJson"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"manual": {
"inputs": {
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {}
}
在這些情況下,我發現編寫 Azure Function 來完成工作要容易得多。
它最終將使您的 LogicApp 比使用一大堆功能來解決(感知的)限制要干凈得多。
在 Azure 門戶中,go 到Azure Functions
刀片並使用以下代碼創建一個新的 .NET HttpTrigger function...
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
string thresholdString = req.Query["threshold"];
var threshold = int.Parse(thresholdString);
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var jObject = JObject.Parse(requestBody);
var filteredObject = new JObject(jObject.Properties().ToList().Where(x => (int)x.Value >= threshold));
return new OkObjectResult(filteredObject);
}
...它假定您傳入的 JSON 與您提供的內容一致,因此請注意這一點。
現在從你的 LogicApp 調用它,你會得到你想要的響應......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.