-2

I am working on a C# project. The following is my JSON. I want to remove the "items":[] from it for "Type":"Product" as it is causing problems on the UI. I tried to exclude the value while JSON is being created by using the following Item class as described at How to Ignoring Fields and Properties Conditionally During Serialization Using JSON.Net?. But it didn't work, not sure why. Alternatively, is there a way to remove it after the JSON is created?

  [
     {
      "Type":"Category",
      "Order":1,
      "id":"24",
      "text":"abc",
      "items":[
         {
            "Type":"Product",
            "Order":0,
            "id":1900,
            "text":"abc product",
            "items":[
               
            ]
         }
      ]
   },
   {
      "Type":"Category",
      "Order":1,
      "id":"6",
      "text":"efg",
      "items":[
         {
            "Type":"Product",
            "Order":0,
            "id":2446,
            "text":"efg Product",
            "items":[
               
            ]
         },
         {
            "Type":"Product",
            "Order":0,
            "id":2447,
            "text":"efg1 Product",
            "items":[
               
            ]
         }
      ]
   }
  ]
    [Serializable]
    public class Item
    {
        public String Type { get; set; }
        public int Order { get; set; }
        public int id { get; set; }
        public string text { get; set; }
        public IList<Item> items { get; set; }

        public bool ShouldSerializeitems()
        {
            return this.Type != "Product";
        }

        public Item()
        {
            items = new List<Item>();
        }
    }
    foreach (Products product in products)
    {
                            
       Item product = new Item();
       product.id = Convert.ToInt32(product.GetProductID());
       product.text = product.GetName();
       product.Order = product.GetProductSortOrder();
       product.Type = "Product";
       Category.items.Add(product);
    }
Kim
  • 53
  • 1
  • 8

1 Answers1

2

Removing data from a JSON string post-serialization is nearly always the wrong approach. You should always handle it during serialization.

In addition, you most likely should be checking if items is an empty collection:

[Serializable]
public class Item
{
    public String Type { get; set; }
    public int Order { get; set; }
    public int id { get; set; }
    public string text { get; set; }
    public IList<Item> items { get; set; }

    public bool ShouldSerializeitems()
    {
        return items.Any();
    }

    public Item()
    {
        items = new List<Item>();
    }
}

This correctly outputs:

[
  {
    "Type": "Category",
    "Order": 1,
    "id": 24,
    "text": "abc",
    "items": [
      {
        "Type": "Product",
        "Order": 0,
        "id": 1900,
        "text": "abc product"
      }
    ]
  },
  {
    "Type": "Category",
    "Order": 1,
    "id": 6,
    "text": "efg",
    "items": [
      {
        "Type": "Product",
        "Order": 0,
        "id": 2446,
        "text": "efg Product"
      },
      {
        "Type": "Product",
        "Order": 0,
        "id": 2447,
        "text": "efg1 Product"
      }
    ]
  }
]
David L
  • 32,885
  • 8
  • 62
  • 93
  • Hi David, I have tried your solution, still "items":[ ] are included under "Product". Actually, I have copied your Item class and used it in my code to avoid any typos. – Kim Aug 25 '21 at 17:15
  • How are you serializing the data? I took your exact json, deserialized it to a list of Item, then re-serialized it and the output worked just fine. If this isn't working for you, you must be doing something else that you haven't highlighted in your question. – David L Aug 25 '21 at 17:25
  • @Kim - are you sure you are using [tag:json.net] and not [tag:system.text.json]? You have tagged your question [tag:json.net] and this answer will work with that serializer, however if you are using [tag:system.text.json] then conditional serialization is not supported. If this answer does not work please [edit](https://stackoverflow.com/posts/68925958/edit) your original question to provide a [mcve]. – dbc Aug 25 '21 at 17:45
  • dbc, you are correct. I was using System.Web.Script.Serialization.JavaScriptSerializer().Serialize(categoryData); by mistake. Once I changed it to JsonConvert.SerializeObject(categoryData); it's working fine. Sorry for the oversight on my part! – Kim Aug 25 '21 at 18:04
  • David, your solution works fine I was using the wrong serializer. Please see my comment above. Thanks! – Kim Aug 25 '21 at 18:05
  • Absolutely do not use that serializer under any circumstances. It is deprecated, slow, and error prone. Glad this works for you with JSON.Net! – David L Aug 25 '21 at 18:05
  • It is better to stop using `JavaScriptSerializer` as it is [deprecated](https://learn.microsoft.com/en-us/dotnet/api/system.web.script.serialization.javascriptserializer?view=netframework-4.8#remarks). It has not been ported to .NET Core, and never will be. – dbc Aug 25 '21 at 18:06