Sort all the JSON
Disclaimer: This tool is designed in mind for checking JSON as part of Quality Assurance for the sake of the human viewing JSON. Your own code should not be reliant on how things are ordered. Also keep in mind that order can matter in JSON and it's up to you to decide how the tool will be used.
The latest stable release of JSorter is available on NuGet or can be downloaded from GitHub.
To order using default configuration is a simple to use extension method
using JSorter;
var a = JObject.Parse(@"{ ""b"" : ""b"", ""a"": ""a"" }");
a = a.Sort();
Console.Write(a.ToString());
{
"a": "a",
"b": "b"
}
By default sorting primitive values in Json is disabled, this is due to the fact that the sequence of primitives may matter
This can be enabled via configuration passed as an additional argument when using the Sort()
method
Example
using JSorter;
using JSorter.Configuration;
jTest = jTest.Sort(new JSorterConfiguration() {
SortPrimitiveValuesInArrays = true
}
var a = JArray.Parse(@"[2,1,3]");
a = a.Sort();
Console.Write(a.ToString());
Outputs
[ 1,2,3 ]
By default, objects belonging to a JSON array are sorted by there string representation. However this may cause problems if the first field changes You can use a selector to use as a sorting value for arrays.
Example
using JSorter;
using JSorter.Configuration;
jTest = jTest.Sort(new JSorterConfiguration() {
SortArrayObjectBy = new List<string>() { "id" }
}
var a = JArray.Parse(@"[{""a"": 1, ""id"":""2""},{""b"": 1, ""id"":""1""}]");
a = a.Sort();
Console.Write(a.ToString());
Outputs
[
{
"b" : 1,
"id": 1
},
{
"a": 1,
"id": 2
}
]
In some scenarios you may not want to sort the properties of an object.
The sorting of properties can be disabled using the SortJsonObjectProperties
flag.
using JSorter;
using JSorter.Configuration;
jTest = jTest.Sort(new JSorterConfiguration() {
SortArrayObjectBy = new List<string>() { "id" },
SortJsonObjectProperties = false
}
var a = JArray.Parse(@"[{""k"": 1, ""id"":""2""},{""l"": 1, ""id"":""1""}]");
a = a.Sort();
Console.Write(a.ToString());
Outputs
[
{
"l" : 1,
"id": 1
},
{
"k": 1,
"id": 2
}
]