When serializing objects using the Netwonsoft.JSON library, it's possible to specify the output order using JsonPropertyAttribute
property Order
. However, I would like to also sort alphabetically the properties by default on top of that.

- 491
- 1
- 5
- 18
-
2Relying on JSON property ordering sounds fishy to me. What's this for? Who will be consuming this "ordered" JSON? If you want to represent an ordering, an array of key-value pairs sounds like the better option to me. – spender Jul 08 '19 at 11:03
-
9The need to sort JSON properties is only to facilitate human diffs between JSON files coming from different components into a central consumer – Michele Di Cosmo Jul 08 '19 at 11:09
-
2I want to also add, I come across today that an API provider requires of me an MD5 digest of the request JSON string to be compared with at their end. Ordering JSON properties is required for an involatile string. Otherwise the message digest won't compare. They don't use key-value pairs mainly because the JSON object isn't flat - it has layers. – Lionet Chen Jan 15 '20 at 01:06
-
5JSON is made to be human readable. So not fishy to want them ordered. Also, it makes any diffs easier to analyse if the system can't order it how it wants. – noelicus Jul 13 '20 at 17:12
-
If you are doing automated comparisons ( eg. verbose assertions ) it reduces future churn when new fields are added. – Fred Haslam Aug 01 '21 at 06:13
-
1Unit testing is another place I've found consistent ordering to be helpful. – broc.seib Oct 05 '22 at 20:28
4 Answers
You can create a custom contract resolver, by extending Newtonsoft.Json.Serialization.DefaultContractResolver
. The CreateProperties
method is the one responsible of the property order, so overriding it, and re-sorting the properties would change the behaviour in the way you want:
public class OrderedContractResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
{
protected override System.Collections.Generic.IList<Newtonsoft.Json.Serialization.JsonProperty> CreateProperties(System.Type type, Newtonsoft.Json.MemberSerialization memberSerialization)
{
var @base = base.CreateProperties(type, memberSerialization);
var ordered = @base
.OrderBy(p => p.Order ?? int.MaxValue)
.ThenBy(p => p.PropertyName)
.ToList();
return ordered;
}
}
In order to use a custom contract resolver you have to create a custom Newtonsoft.Json.JsonSerializerSettings
and set its ContractResolver
to an instance of it:
var jsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings
{
ContractResolver = new OrderedContractResolver(),
};
and then serialize using the above settings object's instance:
using (Newtonsoft.Json.JsonWriter writer = new Newtonsoft.Json.JsonTextWriter(sw))
{
var serializer = Newtonsoft.Json.JsonSerializer.Create(jsonSerializerSettings);
serializer.Serialize(writer, jsonObject);
}
where sw
is a simple string writer:
var sb = new System.Text.StringBuilder();
var sw = new System.IO.StringWriter(sb);
and jsonObject
is the object you wish to serialize.

- 491
- 1
- 5
- 18
-
what's the difference between your using block with the StringBuilder/StringWriter and simply calling JsonConvert.SerializeObject( jsonObject, jsonSerializerSettings )? – laventnc Aug 31 '20 at 15:51
You can actually control the order by implementing IContractResolver
or overriding the DefaultContractResolver's CreateProperties
method.
Here's an example of my simple implementation of IContractResolver
which orders the properties alphabetically:
public class OrderedContractResolver : DefaultContractResolver
{
protected override System.Collections.Generic.IList<JsonProperty> CreateProperties(
System.Type type, MemberSerialization memberSerialization)
{
return base.CreateProperties(type, memberSerialization)
.OrderBy(p => p.PropertyName)
.ToList();
}
}
And then set the settings and serialize the object, and the JSON
fields will be in alphabetical order:
var settings = new JsonSerializerSettings()
{
ContractResolver = new OrderedContractResolver()
};
var json = JsonConvert.SerializeObject(obj, Formatting.Indented, settings);

- 5,381
- 2
- 16
- 41
-
You probably should order the properties using `StringComparer.Ordinal` to avoid culture-specific sorting. I.e. `.OrderBy(p => p.PropertyName, StringComparer.Ordinal)`. – dbc Apr 19 '20 at 19:34
Firstly, if you want to order elements in the code, use the Property Order Attribute. I use this class which will use that property order (if present) and then sorts alphabetically:
public static class JsonHelper
{
public class OrderedContractResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(System.Type type, MemberSerialization memberSerialization)
{
return base.CreateProperties(type, memberSerialization)
.OrderBy(p => p.Order ?? int.MaxValue) // Honour any explit ordering first
.ThenBy(p => p.PropertyName)
.ToList();
}
}
public static string SerialiseAlphabeticaly(object obj)
{
return JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new OrderedContractResolver() });
}
}
Usage:
string json = JsonHelper.SerialiseAlphabeticaly(myObject);

- 14,468
- 3
- 92
- 111
You could define your own ContractResolver:
public class CustomContractResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
return base.CreateProperties(type, memberSerialization).OrderBy(x=>x.PropertyName).ToList();
}
}
With usage:
string json = JsonConvert.SerializeObject(myClass, new JsonSerializerSettings
{
ContractResolver = new CustomContractResolver(),
Formatting = Formatting.Indented
});

- 9,561
- 4
- 36
- 49
-
Thanks. This however overrides the order specified through the JsonProperty attribute – Michele Di Cosmo Jul 08 '19 at 11:11