Home > Mobile >  Get the count of same attributes in json
Get the count of same attributes in json

Time:10-12

I have following json data. How can I get the number of same attributes present in the json file? Suppose if I call the function int count =GetCount(ProductImportData,"ID") then I should return 2 as result. Can you please help me on this?

{
"ProductImportData": {
    "ProductsToImport": [{
            "ImportMode": "Update",
            "DateInfo": {
                "LastModifiedDate": "2022-04-04T17:30:15.8609796Z"
            },
            "Values": [{
                "Name": "ID",
                "ValuesByLocale": {
                    "en-US": "101"
                }
            }]
        },
        {
            "ImportMode": "Update",
            "DateInfo": {
                "LastModifiedDate": "2018-05-05T16:36:41.1106642Z"
            },
            "Values": [{
                "Name": "ID",
                "ValuesByLocale": {
                    "en-US": "102"
                }
            }]
        },
    
         {
            "ImportMode": "Update",
            "DateInfo": {
                "LastModifiedDate": "2016-05-05T16:36:41.1106642Z"
            },
            "Values": [{
                "Name": "Name",
                "ValuesByLocale": {
                    "en-US": "Product Name"
                }
            }]
        }
    ]
}
}

And following is the class I created.

     public class DateInfo
        {
            public DateTime LastModifiedDate { get; set; }
        }

        public class ProductImportData
        {
            public List<ProductsToImport> ProductsToImport { get; set; }
        }

        public class ProductsToImport
        {
            public string ImportMode { get; set; }
            public DateInfo DateInfo { get; set; }
            public List<Value> Values { get; set; }
        }

        public class Root
        {
            public ProductImportData ProductImportData { get; set; }
        }

        public class Value
        {
            public string Name { get; set; }
            public ValuesByLocale ValuesByLocale { get; set; }
        }

        public class ValuesByLocale
        {
            [JsonProperty("en-US")]
            public string EnUS { get; set; }
        }

following is what I tried. But it has some issues.

 public static int GetCount(ProductImportData productImportData, string attribute)
    {
        List<string> values = productImportData.ProductsToImport
                     .Where(pti => pti.Values.Any(v => v.Name == attribute))
                     .Select(pti => pti.Values.Any(k => k.ValuesByLocale.EnUS));
        if (values.Count > 0)
        {
            return values.Count;
        }

    }

please help me on this

CodePudding user response:

try with SelectMany and Where clause

var values = productImportData.ProductsToImport.SelectMany(x=>x.Values)
                .Where(pti => pti.Name == attribute)
                .Select(x => x.ValuesByLocale.EnUS).ToList();

Here is the simplified method if you want only count

public static int GetCount(ProductImportData productImportData, string attribute)
{
    return productImportData.ProductsToImport
                            .SelectMany(x => x.Values)
                            .Where(pti => pti.Name == attribute)
                            .Count();
}

CodePudding user response:

If you were to use Newtonsoft.Json, your task could be realized with basically a one-liner utilizing Newtonsoft.Json's JToken.SelectTokens method. This method utilizes JPath expressions to find elements in a json structure.

using System.Linq;
using Newtonsoft.Json.Linq;

var jsonAsJToken = JToken.Parse(jsonText);
// or alternatively: var jsonAsJToken = JToken.Load( <some JsonReader> );

var numberOfIdNames = jsonAsJToken.SelectTokens("$..*[?(@.Name=='ID')]").Count();

For an explanation of JPath syntax, look here: https://goessner.net/articles/JsonPath/. Brief explanation: $..* selects any element in the json structure, [?(@.Name=='ID')] restricts the selection to elements having a "Name" property (and therefore these elements must be a json object) with a string value equal to "ID".

  • Related