Samithra niroshana
Samithra niroshana

Reputation: 67

How to remove specific attribute from json object c#

My JSON object has a pages array and elements array inside it. elements array has many objects .each objects has correct answer attribute

I want to remove the correct answer attribute from the whole JSON.

I tried in this way but I couldn't remove the correct answer attribute.

    var o = (Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(ob.Json);
                o.Property("pages").Remove();
{
 "pages": [
  {
   "name": "page1",
   "elements": [
    {
     "type": "radiogroup",
     "name": "question1",
     "correctAnswer": "item1",
     "choices": [
      "item1",
      "item2",
      "item3"
     ]
    },
    {
     "type": "radiogroup",
     "name": "question2",
     "correctAnswer": "item2",
     "choices": [
      "item1",
      "item2",
      "item3"
     ]
    },
   ]
}
]
}

Upvotes: 0

Views: 6114

Answers (2)

Anu Viswan
Anu Viswan

Reputation: 18163

You could use JToken.Remove together with Linq to remove the correctAnswers. For example,

var jo = JObject.Parse(json);
var answers = jo.Descendants()
           .Where(x => x.Type == JTokenType.Property)
           .Cast<JProperty>()
           .Where(x=>x.Name=="correctAnswer").ToList();
foreach(var answer in answers)
{
    answer.Remove();
}
var filterJson = jo.ToString(); 

Upvotes: 2

T&#226;n
T&#226;n

Reputation: 1

If you want to remove correctAnswer property inside that json, you can make a loop for each element item and set correctAnswer to null:

using System.Collections.Generic;
using Newtonsoft.Json;

namespace Program
{
    class Element
    {
        public string type { get; set; }
        public string name { get; set; }
        public string correctAnswer { get; set; }
        public IList<string> choices { get; set; }
    }

    class Item
    {
        public string name { get; set; }
        public IList<Element> elements { get; set; }
    }

    class MyClass
    {
        public IList<Item> pages { get; set; }
    }

    class Program
    {
        static void Main()
        {
            var data = "{" +
                "'pages':[" +
                    "{'name':'page1'," +
                    "'elements':[" +
                        "{" +
                            "'type':'radiogroup'," +
                            "'name':'question1'," +
                            "'correctAnswer':'item1'," +
                            "'choices':['item1','item2','item3']" +
                        "}," +
                        "{" +
                            "'type':'radiogroup'," +
                            "'name':'question2'," +
                            "'correctAnswer':'item2'," +
                            "'choices':['item1','item2','item3']" +
                        "}" +
                    "]" +
                    "}]" +
                "}";

            var obj = JsonConvert.DeserializeObject<MyClass>(data);

            foreach (var page in obj.pages)
            {
                foreach (var element in page.elements)
                {
                    element.correctAnswer = null;
                }
            }
        }
    }
}

Another way, in this example, we can remove correctAsnwer property from the class Element. Then the result after parsing the string to json object, correctAnswer property won't be accessed.

class Element
{
    public string type { get; set; }
    public string name { get; set; }
    // public string correctAnswer { get; set; }
    public IList<string> choices { get; set; }
}

Upvotes: 1

Related Questions