Configure NamingStrategy property name serialization JsonPropertyAttribute name This sample uses JsonPropertyAttribute to change the names of properties when they are serialized to JSON. Serializing and Deserializing JSON. The JsonSerializer is able to read and write JSON text directly to a stream via JsonTextReader and JsonTextWriter. Other kinds of JsonWriters can also be used, such as JTokenReader / JTokenWriter, to convert your object to and from LINQ to JSON objects, or BsonReader / BsonWriter, to convert to and from BSON. Serializing and Deserializing JSON. The JsonSerializer is able to read and write JSON text directly to a stream via JsonTextReader and JsonTextWriter. Other kinds of JsonWriters can also be used, such as JTokenReader / JTokenWriter, to convert your object to and from LINQ to JSON objects, or BsonReader / BsonWriter, to convert to and from BSON. However, this uses the property names from data when printing to the.json file. I would like to change the property names to be something different (say, change 'foo' to 'bar'). In the Json.net documentation, under 'Serializing and Deserializing JSON' → 'Serialization Attributes' it says 'JsonPropertyAttribute. Allows the name to be. By default, the JSON property will have the same name as the.NET property. This attribute allows the name to be customized. JsonPropertyAttribute indicates that a property should be serialized when member serialization is set to opt-in. It includes non-public properties in serialization and deserialization.
Active6 months ago
I have some data in a C# DataSet object. I can serialize it right now using a Json.net converter like this
However, this uses the property names from
data
when printing to the .json file. I would like to change the property names to be something different (say, change 'foo' to 'bar').In the Json.net documentation, under 'Serializing and Deserializing JSON' → 'Serialization Attributes' it says 'JsonPropertyAttribute... allows the name to be customized'. But there is no example. Does anyone know how to use a JsonPropertyAttribute to change the property name to something else?
(Direct link to documentation)
Json.net's documentation seems to be sparse. If you have a great example I'll try to get it added to the official documentation.Thanks!
Uwe Keim28.4k3333 gold badges142142 silver badges228228 bronze badges
culixculix5,38255 gold badges2626 silver badges4747 bronze badges
3 Answers
You could decorate the property you wish controlling its name with the
[JsonProperty]
attribute which allows you to specify a different name:Documentation: Serialization Attributes
Martin Brown17.9k1212 gold badges6565 silver badges100100 bronze badges
Darin DimitrovDarin Dimitrov878k233233 gold badges30763076 silver badges27922792 bronze badges
If you don't have access to the classes to change the properties, or don't want to always use the same rename property, renaming can also be done by creating a custom resolver.
For example, if you have a class called
MyCustomObject
, that has a property called LongPropertyName
, you can use a custom resolver like this…Then call for serialization and supply the resolver:
And the result will be shortened to {'Short':'prop value'} instead of {'LongPropertyName':'prop value'}
More info on custom resolvers here
Luke GirvinNewtonsoft Json Serialize Object Name
11.1k88 gold badges5353 silver badges7575 bronze badges
StingyJack![Newtonsoft Newtonsoft](/uploads/1/2/4/8/124873234/319412508.png)
16.1k77 gold badges5252 silver badges105105 bronze badges
There is still another way to do it, which is using a particular NamingStrategy, which can be applied to a class or a property by decorating them with
[JSonObject]
or [JsonProperty]
.There are predefined naming strategies like
CamelCaseNamingStrategy
, but you can implement your own ones.The implementation of different naming strategies can be found here: https://github.com/JamesNK/Newtonsoft.Json/tree/master/Src/Newtonsoft.Json/Serialization
JotaBeJotaBe31.2k66 gold badges6969 silver badges9696 bronze badges