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();
Source: stackoverflow.com
Related Articles
- Replacing an array item based on a property in the item
- linq remove item from object array where property equals value
- Create a subset of an object based off an array of property names
- Explanation of Code : retrieve item from Array using FirstorDefault()
- Finding an item based on its enumerable property
- Create array of property values from list based on condition
- Find an item from collection based on the minimum value of a property
- Searching for an item in a list that's nested inside another list based on a property value in C# using LINQ?
- Sorting Array based on property of object on inner array
- c# Array Property based on other array
- Change binding at runtime to a particular element of a list based off property of the list item
- List or Array of String Contain specific word in Html Source Code
- Linq select array items based on sub class property
- Linq to search a property of a list based on string array variable
- Find subset in List of objects based on matched property in string array
- Entity Framework Code First Select Item Based on Relationship
- Split Item into N Item based on the value of a property
- Linq how to query a list of items for a combined list of a child collection based on a property of the parent item
- Linq code to select one item
- C# - code to order by a property using the property name as a string
- Remove item from list based on condition
- Updating an item property within IEnumerable but the property doesn't stay set?
- Remove Item in Dictionary based on Value
- Convert an array to dictionary with value as index of the item and key as the item itself
- Cannot assign null to anonymous property of type array
- Group List of Objects based on Property using Linq?
- Get the index of item in a list given its property
- C#: Altering values for every item in an array
- How to join two Lists based on common property
- Replacing nested foreach with LINQ; modify and update a property deep within
- Linq fill data for nested List<T>
- C# linq FirstOrDefault()
- Is there a standard LINQ operator that will transform an IEnumerable<T> into another by looking ahead at its own elements
- LINQ query to split an ordered list into sublists of contiguous points by some criteria
- How to avg() in linq
- what is wrong with my GroupBy Query
- Left outer join linq query object reference not set to an instance of an object
- how to convert this T-SQL statment to linq
- C# Find Object's Properties that match a certain criteria
- Find distinct categories of all elements using LINQ
- Implementing Next/Previous with LINQ to SQL
- How to get the value from a XML Document
- LINQ getting distinct records based on an item value
- Compare two object lists with LINQ on specific property
- Converting SQL to LINQ with Multiple Tables and Group
- Use Linq to join an object's list with its ID
- C# Linq Select from one to many but if in the many one is false dont return
- How to Join 2 tables using lambda expression
- conditional include in linq to entities?
- How to iterate dynamically throught a list of (different) objects and get the value of a specific property?