score:1

Accepted answer

Here is an example how you can do it. What you need is access the property you wish and replace a value of it.

void Main()
{
    JObject jObject = JsonConvert.DeserializeObject<JObject>(File.ReadAllText(path));
    JObject newCategory = new JObject();
    newCategory.Add("Name", "Identity");
    newCategory.Add("Mandatory", "false");
    newCategory.Add("NewProp", "Yes");
    ReplaceCategoryValue(jObject, "Identity", newCategory);

    Console.WriteLine(jObject.ToString());
}

void ReplaceCategoryValue(JObject jObject, string categoryName, JObject newCategory)
{
    JToken categories = jObject["Categories"];
    JObject targetCategory = categories.Children<JObject>().FirstOrDefault(x => x.Property("Name").Value.ToString() == categoryName);
    targetCategory.Replace(newCategory);
}

score:1

I still do not understand why you wouldn't define a POCO. Here have a look at this one. Here are your POCOS

public class MyRootObject
{
    public string Name { get; set; }
    public List<Category> Categories { get; set; }
}

public class Category
{
    public string Name { get; set; }
    public bool Mandatory { get; set; }
}

And this is how you use it

var rootObject = JsonConvert.DeserializeObject<MyRootObject>(Resource.Json);
//name comes from somewhere else like in the question
var identity = rootObject.Categories.Single(c => c.Name == name);
identity.Mandatory = GetNewIdentity();

Related Articles