1

I have a pretty big JObject with a lot of nested properties. I use this object to populate some UI where I can make changes to each value. Now to my question: How can i search this JObject for a property(i have the Jtoken path) and change its value? The property can be pretty deep so I need to also search the nested properties. For example:

Property1
Property2
--->NestedProperty1
--->NestedProperty2
    --->Propery im searching for
Property3

Also, the properties are not all of the same type, there are JValues, JObjects, JArrays.

I'm pretty new to JObjects so Im not sure if there is an easy way that I'm just not seeing. Thanks in advance.

Joshua H
  • 75
  • 1
  • 9
  • Are the `Property1` and `NestedProperty1` keys unknown at compile time? Can you use a POCO instead of `JObject`? `JObject` also implements `IDictionary` – ColinM Feb 20 '20 at 11:02
  • I suggest you to deserialize json string to object it's convenience to use but if you still don't want deserialize json to class use LINQ to JSON by follow this https://www.newtonsoft.com/json/help/html/LINQtoJSON.htm – Chanom First Feb 20 '20 at 11:04
  • Use `SelectToken()` as explained in [Searching for a specific JToken by name in a JObject hierarchy](https://stackoverflow.com/q/19645501/3744182) and [What is the JSON.NET equivalent of XML's XPath, SelectNodes, SelectSingleNode?](https://stackoverflow.com/q/1698175/3744182). Then use `Replace()` as shown in [Replace part of a JSON with other (using a string token)](https://stackoverflow.com/q/33045235/3744182) or just the assignment operator as shown in [Querying JSON using SelectTokens? With Newtonsoft.Json.Linq in C#](https://stackoverflow.com/a/46229103/3744182). – dbc Feb 20 '20 at 16:40

0 Answers0