0

I created some classes based on the json data in order to deserialize the json data. I would like to access the sku and the skuName using a lambda expression but i am unable to do so.

So managed to get the skuName, but how do i get sku property? i want to get the skuName property and the associated sku property as well. skuName and sku belong to the same class- SKU class. Thanks for your time

    var productSku = skuIdName.BodyText.products.vendors
         .SelectMany(x => x.listings
        .SelectMany(l => l.skus
        .SelectMany(f => f.skuName

         ))).ToArray();

I am unable to obtain ALL the skus and sdkuName (using lambda) and put it in a list or a dictionary using the sku value as Key and the skuName as the value, as i would later like to store these values in a database. e.g. "sku": "SK8772", "skuName": "Domestic and International Calling Plan", "sku": "SK8265", "skuName": "Audio Conferencing",

     JSON DATA:

 {
   "Result": "Success",
   "BodyText": 
  {
  "products": 
{
  "totalProducts": 510,
  "recordsPerPage": 10,
  "page": 1,
  "totalPages": 51,
  "vendors": [
    {
      "vendorId": "397",
      "vendorName": "Microsoft",
      "listings": [
        {
          "listingName": "Office 365 Enterprise",
          "skus": [
            {
              "sku": "SK10228",
              "skuName": "Microsoft 365 E5 without Audio Conferencing",
              "description": "Pending",
              "zeroValueSku": "t",
              "manufacturerPartNumber": "db5e0b1c9cc3459c9d08c61993959fd3",
              "article": "4090153",
              "vendorMapId": "db5e0b1c-9cc3-459c-9d08-c61993959fd3",
              "billingType": "Monthly",
              "productType": "SaaS",
              "qtyMin": "1",
              "qtyMax": "",
              "addOns": [
                {
                  "sku": "SK8265",
                  "skuName": "Audio Conferencing",
                  "description": "For businesses that need to enable users to dial-in a number to join Skype meetings, or dial-out to bring participants into the meeting. There are base pre-requisites required to purchase this offering.",
                  "zeroValueSku": "t",
                  "qtyMin": "1",
                  "qtyMax": "",
                  "vendorMapId": "c94271d8-b431-4a25-a3c5-a57737a1c909",
                  "manufacturerPartNumber": "c94271d8b4314a25a3c5a57737a1c909",
                  "article": "3873033"
                },
                {
                  "sku": "SK8772",
                  "skuName": "Domestic and International Calling Plan",
                  "description": "For Businesses that need to enable online users to place or receive Domestic and International calls through the Public Switched Telephone Network (PSTN). There are base pre-requisites required to purchase this offering.",
                  "zeroValueSku": "t",
                  "qtyMin": "1",
                  "qtyMax": "",
                  "vendorMapId": "ded34535-507f-4246-8370-f9180318c537",
                  "manufacturerPartNumber": "ded34535507f42468370f9180318c537",
                  "article": "3968760"
                },
              }
              ]
            }
          ]
        }
      ]
    }
  ]
}
},
  "Key": 3298012
}


    public class AddOn
     {
    [Newtonsoft.Json.JsonProperty(PropertyName = "id")]
    public string Id { get; set; }
    public string sku { get; set; }
    public string skuName { get; set; }
    public string description { get; set; }
    public string zeroValueSku { get; set; }
    public string qtyMin { get; set; }
    public string qtyMax { get; set; }
    public string vendorMapId { get; set; }
    public string manufacturerPartNumber { get; set; }
    public string article { get; set; }
    public override string ToString()
    {
        return JsonConvert.SerializeObject(this);
    }
}

public class Sku
{
    public string sku { get; set; }
    public string skuName { get; set; }
    public string description { get; set; }
    public string zeroValueSku { get; set; }
    public string manufacturerPartNumber { get; set; }
    public string article { get; set; }
    public string vendorMapId { get; set; }
    public string billingType { get; set; }
    public string productType { get; set; }
    public string qtyMin { get; set; }
    public string qtyMax { get; set; }
    public List<AddOn> addOns { get; set; }
    public List<string> datacenterLocations { get; set; }
    public List<AddOn2> add { get; set; }
}

public class AddOn2
{
    public string sku { get; set; }
    public string skuName { get; set; }
    public string description { get; set; }
    public string zeroValueSku { get; set; }
    public string qtyMin { get; set; }
    public string qtyMax { get; set; }
    public string manufacturerPartNumber { get; set; }
    public string article { get; set; }

    public List<Sku> sk { get; set; }
}

public class Listing
{
    public string listingName { get; set; }
    public List<Sku> skus { get; set; }
    public List<AddOn2> addOns { get; set; }

    //new
    //public List<AddOn2> addon2s { get; set; }
}

public class Vendor
{
    public string vendorId { get; set; }
    public string vendorName { get; set; }//microsoft
    public List<Listing> listings { get; set; }

   // public List<Sku> skus { get; set; }
}

public class Products
{
    public int totalProducts { get; set; }
    public int recordsPerPage { get; set; }
    public int page { get; set; }
    public int totalPages { get; set; }
    public List<Vendor> vendors { get; set; }
}

public class BodyText
{
    public Products products { get; set; }
}

public class RootObject
{
    public string Result { get; set; }
    public BodyText BodyText { get; set; }
    public int Key { get; set; }
}
  }



   RootObject skuIdName = JsonConvert.DeserializeObject<RootObject>(jsonStorageProducts);
10
  • you are not able to figure out how to write the expression? Or you wrote it and cant get the values correctly? Commented Jun 18, 2019 at 11:28
  • Your Json is invalid. Correcting it, you get the value "Microsoft" that your Linq should return. What is your problem really? Commented Jun 18, 2019 at 11:35
  • I am unable to figure out the expression to get all the sku and skuName Commented Jun 18, 2019 at 11:35
  • Just added a answer.... I didnt write in VS so might be slightly off... should give you an idea on how to get to it using lambda. Commented Jun 18, 2019 at 11:36
  • Remove the FirstOrDefault(). You are explicitly asking it to return only first one if exists. In your JSON there is only one at the moment. Commented Jun 18, 2019 at 11:36

3 Answers 3

2

Not sure you meant this:

void Main()
{
    string jsonStorageProducts = @"
{
   ""Result"": ""Success"",
   ""BodyText"": 
  {
  ""products"": 
{
  ""totalProducts"": 510,
  ""recordsPerPage"": 10,
  ""page"": 1,
  ""totalPages"": 51,
  ""vendors"": [
    {
      ""vendorId"": ""397"",
      ""vendorName"": ""Microsoft"",
      ""listings"": [
        {
          ""listingName"": ""Office 365 Enterprise"",
          ""skus"": [
            {
              ""sku"": ""SK10228"",
              ""skuName"": ""Microsoft 365 E5 without Audio Conferencing"",
              ""description"": ""Pending"",
              ""zeroValueSku"": ""t"",
              ""manufacturerPartNumber"": ""db5e0b1c9cc3459c9d08c61993959fd3"",
              ""article"": ""4090153"",
              ""vendorMapId"": ""db5e0b1c-9cc3-459c-9d08-c61993959fd3"",
              ""billingType"": ""Monthly"",
              ""productType"": ""SaaS"",
              ""qtyMin"": ""1"",
              ""qtyMax"": """",
              ""addOns"": [
                {
                  ""sku"": ""SK8265"",
                  ""skuName"": ""Audio Conferencing"",
                  ""description"": ""For businesses that need to enable users to dial-in a number to join Skype meetings, or dial-out to bring participants into the meeting. There are base pre-requisites required to purchase this offering."",
                  ""zeroValueSku"": ""t"",
                  ""qtyMin"": ""1"",
                  ""qtyMax"": """",
                  ""vendorMapId"": ""c94271d8-b431-4a25-a3c5-a57737a1c909"",
                  ""manufacturerPartNumber"": ""c94271d8b4314a25a3c5a57737a1c909"",
                  ""article"": ""3873033""
                },
                {
                  ""sku"": ""SK8772"",
                  ""skuName"": ""Domestic and International Calling Plan"",
                  ""description"": ""For Businesses that need to enable online users to place or receive Domestic and International calls through the Public Switched Telephone Network (PSTN). There are base pre-requisites required to purchase this offering."",
                  ""zeroValueSku"": ""t"",
                  ""qtyMin"": ""1"",
                  ""qtyMax"": """",
                  ""vendorMapId"": ""ded34535-507f-4246-8370-f9180318c537"",
                  ""manufacturerPartNumber"": ""ded34535507f42468370f9180318c537"",
                  ""article"": ""3968760""
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}
},
  ""Key"": 3298012
}
";
    RootObject skuIdName = JsonConvert.DeserializeObject<RootObject>(jsonStorageProducts);
    var productSku = skuIdName.BodyText.products.vendors
     .SelectMany(x =>x.listings
     .SelectMany(l => l.skus
     .SelectMany(s => s.addOns
     .Select(o => new { 
        SKU = o.sku, 
        Name = o.skuName }))));

    foreach (var sku in productSku)
    {
        Console.WriteLine($"SKU: {sku.SKU}, Name: {sku.Name}");
    }
}


public class AddOn
{
    [Newtonsoft.Json.JsonProperty(PropertyName = "id")]
    public string Id { get; set; }
    public string sku { get; set; }
    public string skuName { get; set; }
    public string description { get; set; }
    public string zeroValueSku { get; set; }
    public string qtyMin { get; set; }
    public string qtyMax { get; set; }
    public string vendorMapId { get; set; }
    public string manufacturerPartNumber { get; set; }
    public string article { get; set; }
    public override string ToString()
    {
        return JsonConvert.SerializeObject(this);
    }
}

public class Sku
{
    public string sku { get; set; }
    public string skuName { get; set; }
    public string description { get; set; }
    public string zeroValueSku { get; set; }
    public string manufacturerPartNumber { get; set; }
    public string article { get; set; }
    public string vendorMapId { get; set; }
    public string billingType { get; set; }
    public string productType { get; set; }
    public string qtyMin { get; set; }
    public string qtyMax { get; set; }
    public List<AddOn> addOns { get; set; }
    public List<string> datacenterLocations { get; set; }
    public List<AddOn2> add { get; set; }
}

public class AddOn2
{
    public string sku { get; set; }
    public string skuName { get; set; }
    public string description { get; set; }
    public string zeroValueSku { get; set; }
    public string qtyMin { get; set; }
    public string qtyMax { get; set; }
    public string manufacturerPartNumber { get; set; }
    public string article { get; set; }

    public List<Sku> sk { get; set; }
}

public class Listing
{
    public string listingName { get; set; }
    public List<Sku> skus { get; set; }
    public List<AddOn2> addOns { get; set; }

    //new
    //public List<AddOn2> addon2s { get; set; }
}

public class Vendor
{
    public string vendorId { get; set; }
    public string vendorName { get; set; }//microsoft
    public List<Listing> listings { get; set; }

    // public List<Sku> skus { get; set; }
}

public class Products
{
    public int totalProducts { get; set; }
    public int recordsPerPage { get; set; }
    public int page { get; set; }
    public int totalPages { get; set; }
    public List<Vendor> vendors { get; set; }
}

public class BodyText
{
    public Products products { get; set; }
}

public class RootObject
{
    public string Result { get; set; }
    public BodyText BodyText { get; set; }
    public int Key { get; set; }
}

Output:

SKU: SK8265, Name: Audio Conferencing
SKU: SK8772, Name: Domestic and International Calling Plan
Sign up to request clarification or add additional context in comments.

Comments

0

Adding the lambda expression that might help you to take the values you require. Below is an example to get list of SKUs.

var root = new RootObject();
var skus = new List<Sku>();
root.BodyText.products.vendors.ForEach(vendor =>
        vendor.listings.ForEach(listing => listing.skus.ForEach(sku => skus.Add(sku))));

The above code will get you the SKUs of all the vendors of all listings and so on. Now using this as an example you should be able to proceed. If you need further help do let me know.

Comments

0

You can use ForEach of LINQ to iterate over the model and populate a dictionary as follows:

var nameIdDict = new Dictionary<string, string>();

skuIdName.BodyText.products.vendors.ForEach(v => 
{
    v.Listings.ForEach(listing => 
    {
        listing.skus.ForEach(s => 
        {
            nameIdDict.Add(s.sku, s.skuName);
            s.addOns.ForEach(a => 
            {
                nameIdDict.Add(a.sku, a.skuName);
            });
            s.add.ForEach(a => 
            {
                nameIdDict.Add(a.sku, a.skuName);
            });
        });
        listing.addOns.ForEach(a => 
        {
            nameIdDict.Add(a.sku, a.skuName);
        });
    });
});

Ultimately you should get the sku and respective skuNames in the dictionary. Though you won't be knowing at which level in the json it existed.

9 Comments

You were close but not quite. Maybe you want to edit your answer to return what he wants: skuIdName.BodyText.products.vendors.ForEach(v => { v.listings.ForEach(listing => { listing.skus.ForEach(s => { s.addOns.ForEach(a => { nameIdDict.Add(a.sku, a.skuName); }); }); }); });
@CetinBasoz close but not quite...why? is it becoz I have added sku and skuNames from other places also?
Nope, just corrected your typos and removed one extra level you added. You can quickly test your code and the corrected one, I already provided the code in my answer. You can copy paste there and test what you get. Your code wouldn't run as is.
@CetinBasoz in your code, you are fetching the 'addOns` within the 'skus' within the 'listings' collection...right? But what about the 'addOns' within 'listings' object and the 'add' within the 'sku' object? these would also have sku and skuName information
I don't understand what you mean, but anyway read what he wants.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.