Home > Mobile >  Remove certain classes from a JSON Object in C#
Remove certain classes from a JSON Object in C#

Time:04-11

I have a JSON Object with multiple classes and values as shown below:

{
    "Class 1": [{
            "Key1": "value1",
            "Key2": "value3"
        },
        {
            "Key3": "value3",
            "Key4": "value4"
        }
    ],
    "Class 2": [{
            "Key11": "value11",
            "Key21": "value31"
        },
        {
            "Key31": "value31",
            "Key41": "value41"
        }
    ],
    "Class 3": [{
            "Key112": "value112",
            "Key212": "value312"
        },
        {
            "Key312": "value312",
            "Key412": "value412"
        }
    ],
    "Class 4": [{
            "Key12": "value12",
            "Key22": "value32"
        },
        {
            "Key32": "value32",
            "Key42": "value42"
        },
        {
            "Key321": "value321",
            "Key421": "value421"
        }
    ]
}

I wanted to remove certain classes entirely and get the rest class in a JSON Object. i.e, I want to take only Class 2 and Class 4 from that Object. Could anyone help me achieve this. Thanks in advance. The expected output is shown below:

{
    "Class 2" : [
    {
        "Key11": "value11",
        "Key21": "value31"
    },
    {
        "Key31": "value31"
        "Key41": "value41"
    }
    ],
    "Class 4" : [
    {
        "Key12": "value12",
        "Key22": "value32"
    },
    {
        "Key32": "value32"
        "Key42": "value42"
    },
    {
        "Key321": "value321"
        "Key421": "value421"
    }
    ]
}

CodePudding user response:

First, your JSON is invalid, there are some missing commas.

Your JSON should be as:

{
    "Class 1" : [
    {
        "Key1": "value1",
        "Key2": "value3"
    },
    {
        "Key3": "value",
        "Key4": "value4"
    }
    ],
    "Class 2" : [
    {
        "Key11": "value11",
        "Key21": "value31"
    },
    {
        "Key31": "value31",
        "Key41": "value41"
    }
    ],
    "Class 3" : [
    {
        "Key112": "value112",
        "Key212": "value312"
    },
    {
        "Key312": "value312",
        "Key412": "value412"
    }
    ],
    "Class 4" : [
    {
        "Key12": "value12",
        "Key22": "value32"
    },
    {
        "Key32": "value32",
        "Key42": "value42"
    },
    {
        "Key321": "value321",
        "Key421": "value421"
    }
    ]
}

And your JSON is an object, or also can be recognized as Dictionary<string, List<object>> type.

Concept:

  1. Deserialize as Dictionary<string, List<object>> type.
  2. Filter the x.Key by remainedKeys.
  3. Cast the filtered result to the Dictionary/key-value pair.
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

List<string> remainedKeys = new List<string> { "Class 2", "Class 4" };

var result = JObject.Parse(json_string)
    .ToObject<Dictionary<string, List<dynamic>>>()
    .Where(x => remainedKeys.Contains(x.Key))
    .ToDictionary(x => x.Key, x => x.Value);

Sample Program

CodePudding user response:

Even as the @Yong Shun answer is the correct way to do it (mapping the Object) You can take a roundabout and do a Deep clone serializing/deserializing (it could seem be a little hacky, but it works)

 CompleteClass test; 
 //Fill your data
 LimitedClass result = JsonConvert.DeserializeObject<LimitedClass>(JsonConvert.SerializeObject(test));

then you can serialize (again) result and obtain a JSON text to send to your file

Example classes definition:

    public class Class1
    {
        public string Key1 { get; set; }
        public string Key2 { get; set; }
        public string Key3 { get; set; }
        public string Key4 { get; set; }
    }

    public class Class2
    {
        public string Key11 { get; set; }
        public string Key21 { get; set; }
        public string Key31 { get; set; }
        public string Key41 { get; set; }
    }

    public class Class3
    {
        public string Key112 { get; set; }
        public string Key212 { get; set; }
        public string Key312 { get; set; }
        public string Key412 { get; set; }
    }

    public class Class4
    {
        public string Key12 { get; set; }
        public string Key22 { get; set; }
        public string Key32 { get; set; }
        public string Key42 { get; set; }
        public string Key321 { get; set; }
        public string Key421 { get; set; }
    }

    public class CompleteClass
    {
        public List<Class1> Class1 { get; set; }

        public List<Class2> Class2 { get; set; }

        public List<Class3> Class3 { get; set; }

        public List<Class4> Class4 { get; set; }
    }


    public class LimitedClass
    {
        public List<Class2> Class2 { get; set; }
        public List<Class4> Class4 { get; set; }
    }

CodePudding user response:

try this

     string[] classesNeeded = new string[] { "Class 2", "Class 4" };
    var jsonParsed = JObject.Parse(json);

    jsonParsed.Properties()
   .Where(p => !classesNeeded.Contains(p.Name))
   .ToList()
   .ForEach(prop => prop.Remove());

    json = jsonParsed.ToString();
  • Related