Search code examples
azure-logic-apps

How to check json and do filtering in Logic App


In my Logic App one of the action item gives me Json value as like this . This is recorded in compose action.

{
  "device1": 24,
  "device2": 25,
  "device3": 26
}

I would like to take only that device name and value whose value is equal to and above 25 (in this case device2 and device3)and then pass that value in subsequent method like creating and sending an alert message for each device name whose value is 25 or more.

How can I do that in logic app?


Solution

  • In these situations, I find it much easier to write an Azure Function that does the work.

    It'll ultimately keep your LogicApp cleaner than it would otherwise be with a whole heap of functionality to work around (perceived) limitations.

    In the Azure Portal, go to the Azure Functions blade and create a new .NET HttpTrigger function with the following code ...

    #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);
    }
    

    ... it assumes the JSON you pass in is along the lines of what you provided so be aware of that.

    Now call it from your LogicApp like and you'll get the response you desire ...

    Action

    Action

    Result

    Result