'Is there a way to dynamically filter the JSON?

I am searching a way to filter the JObject according to the request from URL. Second code block works as expected and removes the "Id" column from response:

My JObject:

JObject rss = new(
                    new JProperty("Name", name),
                    new JProperty("Surname",
                         new JArray(from p in data select p.sname)),
                    new JProperty("Id",
                         new JArray(from p in data select p.id)))

What I have tried:

var fields = rss.Descendants()
                           .Where(x => x.Type == JTokenType.Property)
                           .Cast<JProperty>()
                           .Where(x => x.Name == "Id").ToList();
                foreach (var field in fields)
                {
                    field.Remove();
                } 

But, I could not find a way to do this process for requested fields to be removed or return only desired fields. Is there any way to do this type of process except GraphQL?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source