0

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"
     ]
    },
   ]
}
]
}
1

2 Answers 2

2

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(); 
Sign up to request clarification or add additional context in comments.

Comments

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; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.