23

I'm trying to update the value of JToken but its reference is not getting updated.

JSON string:

    {
  "Title": "master",
  "Presentation": [
    {      
      "Component": {
        "Content": {
          "Title": "Set New Title",
        }
      }
    }
  ]
}

and the usage is given below

JObject jo = JObject.Parse(File.ReadAllText(file.json));
foreach (var token in jo.SelectTokens("$..Component.Content").Children())
            {
                JProperty prop = token.ToObject<JProperty>();
                prop.Value = "New Title";
            }
string jsonText = JsonConvert.SerializeObject(jo, Formatting.Indented);

In this example, I'm trying to update the value of Title property. It is getting updated within foreach, means local variable is getting updated but changes are not reflecting in main jobject.

Can anyone help me if i'm doing anything wrong?

3 Answers 3

21

Once you call ToObject then you are working with a copy. If instead you try this, it should work:

JObject jo = JObject.Parse(File.ReadAllText(file.json));
foreach (var prop in jo.SelectTokens("$..Component.Content")
    .Children().OfType<JProperty>())
{
    prop.Value = "New Title";
}
string jsonText = JsonConvert.SerializeObject(jo, Formatting.Indented);

or to handle multiple types of JTokens:

JObject jo = JObject.Parse(File.ReadAllText(file.json));
foreach (var token in jo.SelectTokens("$..Component.Content")
    .Children())
{
    var prop = token as JProperty;
    if (prop != null) prop.Value = "New Title";
    var array = token as JArray;
    if (array != null)
    {
        // some other logic
    }
}

string jsonText = JsonConvert.SerializeObject(jo, Formatting.Indented);
3
  • One more quick question. My JSON object can have property or array type also. Is there anyway to get generic type from its children? Or else should i have to put two loops? one for jproperty and another for jarray?
    – Balaji
    Jan 12, 2017 at 1:04
  • Not entirely sure if this solution works. If your token has an array, your prop variable will still not be null and now your prop will be assigned a string value. It will never get to the array logic.
    – m0g
    May 17, 2019 at 19:44
  • Both code snippets filter for JProperty tokens, declaratively in the first one, and imperatively in the second. Which has the bug?
    – Stuart
    May 19, 2019 at 11:57
2

The answer from Stuart may be erroneous because "Content" may contain other children and all of theme could be renamed or their values could be changed.

I've encountered the similar issue. From the body I needed to remove value, because it was too long for logging and unnecesary, so I needed to change it's value. I could've changed it with indexer like token["name"], but "name" could be of different cases, so I needed an universal case independent way to erase it's value:

And I implemented it other way:

var jObject = JObject.Parse(body);

JToken token;
if (jObject.TryGetValue(
    Constants.FieldName,
    StringComparison.InvariantCultureIgnoreCase,
    out token))
{
    var jProperty = token.Parent as JProperty;
    if (jProperty != null)
    {
        jProperty.Value = "removed";
    }

    body = jObject.ToString(Formatting.Indented);
}
2

Here you have a magnificient example of how you can do this, in a proper way. Both solutions above didn't work for me, but this very simple yes. Simply work with the JToken or Object as an array. That's all.

https://www.newtonsoft.com/json/help/html/ModifyJson.htm

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.