Close

Azure Function to filter an array

I couldn’t get the Filter Array API App working. It just didn’t filter. I also had the problem that I wanted to use multiple conditions to filter the array. I think you can’t accomplish that with the Filter Array API App. For those reasons I decided to write a Azure Function using the Generic Webhook – C# template. To generate the typed object, in this case AGA, I used online resource json2csharp.com.

#r “Newtonsoft.Json”

using System;
using System.Net;
using Newtonsoft.Json;

public class AGA
{
public int ReferenceId { get; set; }
public string Reference { get; set; }
public object Details { get; set; }
public string IntegrationId { get; set; }
public string FormCode { get; set; }
public string KlicNumber { get; set; }
public int RegionId { get; set; }
public string RegionCode { get; set; }
public string RegionName { get; set; }
public string Address { get; set; }
public string ModifiedOn { get; set; }
public int ModifiedById { get; set; }
public string ActionType { get; set; }
public string ActionDescription { get; set; }
public int EntryType { get; set; }
public int State { get; set; }
public int? NewState { get; set; }
public object InspectionDate { get; set; }
public string FieldWorkDate { get; set; }
public string CompanyName { get; set; }
public string TemplateName { get; set; }
public object Technicians { get; set; }
public object Inspectors { get; set; }
}

public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info($”Webhook was triggered!”);

string jsonContent = await req.Content.ReadAsStringAsync();
AGA[] inputAGAs = JsonConvert.DeserializeObject<AGA[]>(jsonContent);

AGA[] outputAGAs = new AGA[100];
int i=0;

foreach(AGA aga in inputAGAs)
{
if (aga.State == 4 && aga.ActionType == “Onderdeel is goedgekeurd”)
{
log.Info($”AGA ” + aga.IntegrationId + ” selected”);
outputAGAs[i] = aga;
i=i+1;
}
}

if (i == 0) {
return req.CreateResponse(HttpStatusCode.BadRequest, new {
error = “No AGA events to be processed”
});
}

return req.CreateResponse(HttpStatusCode.OK, outputAGAs);
}