11

I've the following property

{
  "bad": 
  {
    "Login": "someLogin",
    "Street": "someStreet",
    "House": "1",
    "Flat": "0",
    "LastIndication": 
    [
      "230",
      "236"
    ],
    "CurrentIndication": 
    [
      "263",
      "273"
    ],
    "Photo": 
    [
      null,
      null
    ]
  }
}

and how can I rename this from 'bad' to 'good' for example. Yes, I saw the extension method by Abi Bellamkonda

public static class NewtonsoftExtensions
{
    public static void Rename(this JToken token, string newName)
    {
        var parent = token.Parent;
        if (parent == null)
            throw new InvalidOperationException("The parent is missing.");
        var newToken = new JProperty(newName, token);
        parent.Replace(newToken);
    }
}

but it got this exeption

Can not add Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JProperty.

Umair M
  • 10,298
  • 6
  • 42
  • 74
NisuSan
  • 197
  • 1
  • 13
  • Possible duplicate of [Changing Key Of An Property in Json](https://stackoverflow.com/questions/40002773/changing-key-of-an-property-in-json) – Talha Talip Açıkgöz Nov 13 '17 at 15:18
  • Seems cool, but its does not work. In this example, root obj its JObject. But in my case, I have some array of JProperty's and using them in foreach like this `JArray curLog = JArray.Parse(currentJsonLog); curLog.Children().ToList() .ForEach(o => o.Properties().ToList() .ForEach(p => { if (p.Name == "bad") { //p.ChangeKey("newName");}` – NisuSan Nov 13 '17 at 17:38

1 Answers1

16

Somewhat counterintuitively, that extension method assumes that the token you are passing to it is the value of a JProperty, not the JProperty itself. Presumably, this is to make it easy to use with the square bracket syntax:

JObject jo = JObject.Parse(json);
jo["bad"].Rename("good");

If you have a reference to the property, you can still use that extension method if you call it on the property's Value like this:

JObject jo = JObject.Parse(json);
JProperty prop = jo.Property("bad");
prop.Value.Rename("good");

However, that makes the code seem confusing. It would be better to improve the the extension method so that it will work in both situations:

public static void Rename(this JToken token, string newName)
{
    if (token == null)
        throw new ArgumentNullException("token", "Cannot rename a null token");

    JProperty property;

    if (token.Type == JTokenType.Property)
    {
        if (token.Parent == null)
            throw new InvalidOperationException("Cannot rename a property with no parent");

        property = (JProperty)token;
    }
    else
    {
        if (token.Parent == null || token.Parent.Type != JTokenType.Property)
            throw new InvalidOperationException("This token's parent is not a JProperty; cannot rename");

        property = (JProperty)token.Parent;
    }

    // Note: to avoid triggering a clone of the existing property's value,
    // we need to save a reference to it and then null out property.Value
    // before adding the value to the new JProperty.  
    // Thanks to @dbc for the suggestion.

    var existingValue = property.Value;
    property.Value = null;
    var newProperty = new JProperty(newName, existingValue);
    property.Replace(newProperty);
}

Then you can do:

JObject jo = JObject.Parse(json);
jo.Property("bad").Rename("good");  // works with property reference
jo["good"].Rename("better");        // also works with square bracket syntax

Fiddle: https://dotnetfiddle.net/RSIdfx

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • 1
    Note that `new JProperty(newName, property.Value);` will cause the `property.Value` hierarchy to get cloned, since a `JToken` can have only one parent; see https://dotnetfiddle.net/R0h6wE. You might want to set `property.Value` to `null` beforehand like so: https://dotnetfiddle.net/u5aUnj – dbc Oct 30 '19 at 17:58
  • 1
    @dbc Thanks, I've updated my answer to incorporate your suggestion. – Brian Rogers Oct 30 '19 at 22:44
  • 1
    Great extension method! Works like a charm – Stan Fieuws Jan 23 '20 at 08:15