3

I am trying to read a JSON file, rename the property names, and export a new JSON with the new names. As stated in this example, https://www.newtonsoft.com/json/help/html/JsonPropertyName.htm, we can use JsonProperty to specify a different name internally in the code. However, when you export the json, it returns the original name. So in the example it still returned "release_date" instead of "ReleaseDate" when it was logged in the console. Is there any way to do this without creating a brand new object?

To clear things up, here is example of what I am trying to do:

JSON Input:

{ "name": "Starcraft", "release_date": "1998-01-01T00:00:00" } 

Object Used to deserialize the data:

public class Videogame { public string name{ get; set; } [JsonProperty("release_date")] public DateTime releaseDate { get; set; } } 

Code that is called:

var json = JsonConvert.DeserializeObject<Videogame>(File.ReadAllText(path)) Console.WriteLine(JsonConvert.SerializeObject(json)); 

Resulted Output:

{ "name": "Starcraft", "release_date": "1998-01-01T00:00:00" } 

Desired Output:

{ "name": "Starcraft", "releaseDate": "1998-01-01T00:00:00" } 

The only way that I currently know how to solve it is to create a new object and use it to serialize my output. Wasn't sure if there is any simpler way to do this.

7

2 Answers 2

4

The following will work:

public class Videogame { public string name{ get; set; } [JsonProperty("release_date")] public DateTime old { set { ReleaseDate = value; } } [JsonProperty("releaseDate")] public DateTime ReleaseDate { get; set; } } 

When deserializing, the old property will redirect its value to the new property.

Since the old property is a setter only, it will not be serialized back.

I would not consider this approach for large amounts of renaming. It's then better to follow the advices given in the comments.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice solution, easy to implement
2

You could use custom resolve to create desired behaviour:

public class CustomContractResolver : DefaultContractResolver { public bool UseJsonPropertyName { get; } public CustomContractResolver(bool useJsonPropertyName) { UseJsonPropertyName = useJsonPropertyName; } protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) { var property = base.CreateProperty(member, memberSerialization); if (!UseJsonPropertyName) property.PropertyName = property.UnderlyingName; return property; } } public class ErrorDetails { public int Id { get; set; } [JsonProperty("error_message")] public string ErrorMessage { get; set; } } var json = "{'Id': 1,'error_message': 'An error has occurred!'}"; var serializerSettings = new JsonSerializerSettings() { ContractResolver = new CustomContractResolver(false) }; var dezerializerSettings = new JsonSerializerSettings { ContractResolver = new CustomContractResolver(true) }; var obj = JsonConvert.DeserializeObject<ErrorDetails>(json, dezerializerSettings); var jsonNew = JsonConvert.SerializeObject(obj, serializerSettings); 

Kudos to NtFreX and his answer. I'm providing mine so that you can indicate that your question is resolved.

2 Comments

@dbc Actually I think it would be the best to leave duplicate badge and just delete my answer as it's really similar. Gonna do it in a couple of minutes.
Oh, actually I can't do that. Will include the original author then.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.