简体   繁体   中英

JSON Object in Azure Logic Apps

Someone please help.

We receive orders in different formats and convert to JSON for processing and transformation to our API.

Please see example:

[
  {
    "Order Number": "10188",
    "Order Date": "05/06/2021",
    "SKU": "LW80S",
    "Quantity": "3"
  },
  {
    "Order Number": "10187",
    "Order Date": "05/06/2021",
    "SKU": "LW90L",
    "Quantity": "1"
  },
  {
    "Order Number": "10187",
    "Order Date": "05/06/2021",
    "SKU": "LW80S",
    "Quantity": "1"
  },
  {
    "Order Number": "10187",
    "Order Date": "05/06/2021",
    "SKU": "CCDW12",
    "Quantity": "1"
  },
  {
    "Order Number": "10187",
    "Order Date": "05/06/2021",
    "SKU": "CSS",
    "Quantity": "1"
  }
]

As you can see as there multiple lines for the same order "10187" the conversion to JSON has created multiple objects.

So I need to take the output above and transform to the below:

"Order": {
      "OrderNumber": "10187",
      "TotalUnits": "4",
      "OrderLine": [
         {
            "OrderedQty": "1",
            "Product": "LW90L",
         },
                  {
            "OrderedQty": "1",
            "Product": "LW80S",
         },
                  {
            "OrderedQty": "1",
            "Product": "CCDW12",
         },
        {
            "OrderedQty": "1",
            "Product": "CSS",
         }
      ]
      }

So it is grouped by order number. Can you please advise how I can achieve?

First construct a Dictionary<string, List<OrderItem>> acting as a lookup to store the groupings the key being Order Number , then post doing that just iterate and map it to the original shape which you want.

Here's a working .NET fiddle .

using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
                    
public class Order
{
    [JsonProperty("Order Number")]
    public string OrderNumber { get; set; }
    
    [JsonProperty("Order Date")]
    public string OrderDate { get; set; }
    
    [JsonProperty("SKU")]
    public string SKU { get; set; }
    
    [JsonProperty("Quantity")]
    public string Quantity { get; set; }
}

public class OrderBase
{
    public OrderItem Order { get; set; }
}
public class OrderItem
{
    public string OrderNumber { get; set; }
    public string TotalUnits { get; set; }
    public IEnumerable<OrderLine> OrderLines { get; set; }
}
public class OrderLine
{
    public string OrderedQty { get; set; }
    public string Product { get; set; }
}

public class Program
{

    public static void Main(string[] args)
    {
        string input = @"[{""Order Number"":""10188"",""Order Date"":""05/06/2021"",""SKU"":""LW80S"",""Quantity"":""3""},{""Order Number"":""10187"",""Order Date"":""05/06/2021"",""SKU"":""LW90L"",""Quantity"":""1""},{""Order Number"":""10187"",""Order Date"":""05/06/2021"",""SKU"":""LW80S"",""Quantity"":""1""},{""Order Number"":""10187"",""Order Date"":""05/06/2021"",""SKU"":""CCDW12"",""Quantity"":""1""},{""Order Number"":""10187"",""Order Date"":""05/06/2021"",""SKU"":""CSS"",""Quantity"":""1""}]";
        
        List<Order> orders = JsonConvert.DeserializeObject<List<Order>>(input);
        Dictionary<string, List<OrderLine>> lookup = new Dictionary<string, List<OrderLine>>();
        
        // Group into lookup
        foreach (Order item in orders)
        {
            OrderLine newItem = new OrderLine
            {
                Product = item.OrderNumber,
                OrderedQty = item.Quantity
            };
            
            if (!lookup.ContainsKey(item.OrderNumber))
            {
                lookup[item.OrderNumber] = new List<OrderLine> { newItem };
            }
            else
            {
                lookup[item.OrderNumber].Add(newItem);
            }
        }
        
        // Mapping the groupings to the desired shape in the output
        List<OrderBase> results = new List<OrderBase>();
        foreach (var kvp in lookup)
        {
            results.Add(
                new OrderBase
                {
                    Order = new OrderItem
                    {
                        OrderNumber = kvp.Key,
                        TotalUnits = kvp.Value.Count.ToString(),
                        OrderLines = kvp.Value
                    }
                }
            );
        }
        
        Console.WriteLine("Response - {0}", JsonConvert.SerializeObject(results, Formatting.Indented));
    }
}

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