Home > Software design >  how to move duplicate json objects array in c#
how to move duplicate json objects array in c#

Time:11-03

{
    "data": [
        {
            "country": "China",
            "color" : "Red",
            "pet" : "Cat",
            "name" : "Mark",
            "height_unit_name" : "cm"
        },
        {
            "country": "China",
            "color" : "black",
            "pet" : "dog",
            "name" : "Jane",
            "height_unit_name" : "cm"
        }
    ]
}

I would like to move the duplicate data outside each array and show it once like this... Like "country" and "height_unit_name", both are same in each array.

{
    "country": "China",
    "height_unit_name": "cm",
    "data": [
        {           
            "color": "Red",
            "pet": "Cat",
            "name": "Mark"
        },
        {
            "color": "black",
            "pet": "dog",
            "name": "Jane"
        }
    ]
}

Thankyou for your helping

CodePudding user response:

what you actually want to do is:

  1. deserialize the JSON in some kind of structured object. I've used 'RawData' and RawItem in my example.

          string json = "{    \"data\": [        {            \"country\": \"China\",            \"color\" : \"Red\",            \"pet\" : \"Cat\",            \"name\" : \"Mark\",            \"height_unit_name\" : \"cm\"        },        {            \"country\": \"China\",            \"color\" : \"black\",            \"pet\" : \"dog\",            \"name\" : \"Jane\",            \"height_unit_name\" : \"cm\"        }    ]}";
         RawData rawData = JsonConvert.DeserializeObject<RawData>(json);
    
  2. Once you have the raw data as a list you can do a GroupBy. In your case you group by country and color.

    List<Item> items = new List<Item>();
    var groupedData = rawData.data.GroupBy(x => new { country = x.country, color = x.color });
    
  3. Then you create your desired object structure.

      foreach (var item in groupedData)
      {
          List<Data> data = new List<Data>();
    
          foreach (var dataItem in item)
          {
              data.Add(new Data { pet = dataItem.pet, name = dataItem.name, height_unit_name = dataItem.height_unit_name });
          }
          items.Add(new Item { country = item.Key.country, color = item.Key.color, data = data });
      }
    
  4. Serialize the newly created structure.

    Console.WriteLine(JsonConvert.SerializeObject(items));
    

full running example on .net fiddle: https://dotnetfiddle.net/s4Q4LV

and if you are more the java script "i don't give a f*** about type safetey" guy: https://dotnetfiddle.net/8xE4Du

CodePudding user response:

you can use linq for this

var jsonArr = (JArray)JObject.Parse(json)["data"];

List<CountryPets> pets = jsonArr
    .GroupBy(a => new { c = (string)a["country"], h = (string)a["height_unit_name"], })
    .Select(b => new CountryPets
    {
        Country = b.Key.c,
        HeightUnitName = b.Key.h,
        Pets = b.Select(a => a.ToObject<Pet>()).ToList()
    }).ToList();

public partial class CountryPets
{
    public string Country { get; set; }
    public string HeightUnitName { get; set; }
    public List<Pet> Pets { get; set; }
}

public partial class Pet
{
    [JsonProperty("color")]
    public string Color { get; set; }

    [JsonProperty("pet")]
    public string PetType { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }
}
  • Related