Home > database >  How to get a nested json result after grouping a list?
How to get a nested json result after grouping a list?

Time:04-21

I want to convert following list to JSON result by grouping the list by Category, Merchants and Vouchers (JSON result also shown below). I tried to solve this issue using LINQ queries, but could not do it.

Need to convert following List to JSON result.

    var list = new List<Data>() { 
    new Data { categoryCode = "1", categoryName = "A", merchantId = "1", mechantName = "Shehan", voucherId = "200", voucherDescription = "Voucher - 200"}, 
    new Data { categoryCode = "1", categoryName = "A", merchantId = "1", mechantName = "Shehan", voucherId = "300", voucherDescription = "Voucher - 300"}, 
    new Data { categoryCode = "1", categoryName = "A", merchantId = "2", mechantName = "Ashen", voucherId = "200", voucherDescription = "Voucher - 200"},
    new Data { categoryCode = "2", categoryName = "B", merchantId = "1", mechantName = "Shehan", voucherId = "200", voucherDescription = "Voucher - 200"},
    new Data { categoryCode = "2", categoryName = "B", merchantId = "1", mechantName = "Shehan", voucherId = "300", voucherDescription = "Voucher - 300"},
    new Data { categoryCode = "2", categoryName = "B", merchantId = "2", mechantName = "Ashen", voucherId = "200", voucherDescription = "Voucher - 200"},
    new Data { categoryCode = "2", categoryName = "B", merchantId = "2", mechantName = "Ashen", voucherId = "300", voucherDescription = "Voucher - 300"},
};

This is the Data Model used in the list.

class Data
{
    public string categoryCode { get; set; }
    public string categoryName { get; set; }
    public string merchantId { get; set; }
    public string mechantName { get; set; }
    public string voucherId { get; set; }
    public string voucherDescription { get; set; }
}

Need to get the following JSON Result from the given list.

"Data": [
   {
      "categoryCode":"1",
      "categoryName":"A",
      "merchants":[
         {
            "merchantId":"1",
            "merchantName":"Shehan",
            "vouchers":[
               {
                  "Id":"200",
                  "description":"Voucher - 200"
               },
               {
                  "Id":"300",
                  "description":"Voucher - 300"
               }
            ]
         },
         {
            "merchantId":"2",
            "merchantName":"Ashen",
            "vouchers":[
               {
                  "Id":"200",
                  "description":"Voucher - 200"
               }
            ]
         }
      ]
   },
   {
      "categoryCode":"2",
      "categoryName":"B",
      "merchants":[
         {
            "merchantId":"1",
            "merchantName":"Shehan",
            "vouchers":[
               {
                  "Id":"200",
                  "description":"Voucher - 200"
               },
               {
                  "Id":"300",
                  "description":"Voucher - 300"
               }
            ]
         },
         {
            "merchantId":"2",
            "merchantName":"Ashen",
            "vouchers":[
               {
                  "Id":"200",
                  "description":"Voucher - 200"
               },
               {
                  "Id":"300",
                  "description":"Voucher - 300"
               }
            ]
         }
      ]
   }
]

Really appreciate if you could help on this. :)

CodePudding user response:

I think you can do that by this way:

 var result = list.GroupBy(x => new { x.categoryCode, x.categoryName })
                .Select(x => new
                {
                    x.Key.categoryCode,
                    x.Key.categoryName,
                    Merchants = x.ToList().GroupBy(g => new { g.merchantId, g.mechantName })
                        .Select(v => new
                        {
                            v.Key.merchantId,
                            v.Key.mechantName,
                            vouchers = v.Select(s => new { Id = s.voucherId, description = s.voucherDescription })
                                .ToList()
                        })
                });

            var json = JsonConvert.SerializeObject(result);
  • Related