skip to Main Content

I have a JArray from a json-file like this:

[
  {
    "pos": 36,
    "name": "name1",
    "id": 1
  },
  {
    "pos": 36,
    "name": "name2",
    "id": 2
  },
  {
    "pos": 36,
    "name": "name2",
    "id": 3
  }
]

And I need save json back with unique elements filtered by set of fields. The set of fields can be any.

Update In real file I have 3000 elements in the JArray and about 20 fields. I have no idea how to solve this problem.

Example 1 If I filter the JArray by name I expect to get

[
   {
     "pos": 36,
     "name": "name1",
     "id": 1
   },
   {
     "pos": 36,
     "name": "name2",
     "id": 2
   }
]

Example 2 If I filter the JArray by "name" and "pos" I expect to get

[
   {
     "pos": 36,
     "name": "name1",
     "id": 1
   }
]

2

Answers


  1. Chosen as BEST ANSWER
    public class PropertyModel
    {
        public string ShortName { get; set; }
    
        public bool IsToProcess { get; set; }
    }
    
    List<PropertyModel> SummaryStructure = new List<PropertyModel>();
    
    JArray SummaryIn = JArray.Parse(jsonString);    
    
    var model = SummaryIn[0];
    
    var props = model.Cast<JProperty>();
    
    foreach (var p in props)
    {
        var property = new PropertyModel(){ ShortName = p.Name};
    
        SummaryStructure.Add(property);
    }
    
    var SummaryOut = SummaryIn.ToList();
    
    foreach (PropertyModel item in SummaryStructure)
    {
        if (item.IsToProcess)
        {
            SummaryOut = SummaryOut.GroupBy(p => p[item.ShortName])
                                   .Select(p => p.First())
                                   .ToList();
        }
    }
    

    It was easy :). Maybe someone knows how to pack it in one linq?


  2. You can filter with this code without serializing(with Newtonsoft.Json.Linq;)

    first find unique element and then Serialize to json(Newtonsoft.Json)

    If you want to group several, I realized something from your example, you can write this code

    var newLists = List.GroupBy(x => new { b = x["name"] }).Select(x => x.First())
     .GroupBy(x => new { b = x["pos"] }) .Select(x => x.First()).ToList();
    //convert to string json 
    var str1 = JsonConvert.SerializeObject(newLists);
    

    Result

    [{
    "pos":36,
    "name":"name1",
    "id":1
    }]
    

    If you want to group only once(Really distinct ), use this code

    var newList = List.GroupBy(x => new { b = x["name"], a = x["pos"] })
    .Select(x => x.First()).ToList();
    //convert to string json 
    var str1 = JsonConvert.SerializeObject(newList);
    

    Result:

    [{
    "pos":36,
    "name":"name1",
    "id":1
    },
    {
    "pos":36,
    "name":"name2",
    "id":2
    }]
    

    Base Code:

    JArray List = new JArray();
    List = JArray.Parse(@"[
      {
        ""pos"": 36,
        ""name"": ""name1"",
        ""id"": 1
      },
      {
        ""pos"": 36,
        ""name"": ""name2"",
        ""id"": 2
      },
      {
        ""pos"": 36,
        ""name"": ""name2"",
        ""id"": 3
      }
    ]");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search