C # Sorting JSON String Keys

I would like to convert a JSON string

"{ \"birthday\": \"1988-03-18\", \"address\": { \"state\": 24, \"city\": 8341, \"country\": 1 } }" 

to

 "{ \"address\": { \"city\": 8341, \"country\": 1, \"state\": 24 }, \"birthday\": \"1988-03-18\" }" 

NOTE. I don't use the sorted version for communication (because the order of the keys doesn't really matter), I need a sorted version to run local tests (by comparing JSON strings).


EDIT: I4V pointed out a solution that uses Json.Net , I would prefer to use a solution, t you need to include any third-party library (in fact I use the built-in System.Json in my application)


I posted the gist with the solution provided by I4V + for testing here . Thanks to everyone.

+8
json string sorting c #
source share
3 answers

I will use Json.Net for this

 string json = @"{ ""birthday"": ""1988-03-18"", ""address"": { ""state"": 24, ""city"": 8341, ""country"": 1 } }"; var jObj = (JObject)JsonConvert.DeserializeObject(json); Sort(jObj); string newJson = jObj.ToString(); 

 void Sort(JObject jObj) { var props = jObj.Properties().ToList(); foreach (var prop in props) { prop.Remove(); } foreach (var prop in props.OrderBy(p=>p.Name)) { jObj.Add(prop); if(prop.Value is JObject) Sort((JObject)prop.Value); } } 

EDIT

Try with System.Json , but I'm not sure about OrderByDescending (or OrderBy ).

 var jObj = (System.Json.JsonObject)System.Json.JsonObject.Parse(json); Sort2(jObj); var newJson = jObj.ToString(); 

 void Sort2(System.Json.JsonObject jObj) { var props = jObj.ToList(); foreach (var prop in props) { jObj.Remove(prop.Key); } foreach (var prop in props.OrderByDescending(p => p.Key)) { jObj.Add(prop); if (prop.Value is System.Json.JsonObject) Sort2((System.Json.JsonObject)prop.Value); } } 
+10
source share

Using this approach, you can get a dynamic object with json data

In DynamicJsonConverter create instead of SortedDictionary

 var d = new SortedDictionary<string, object>(dictionary); // TODO: code to sort inner objects return new DynamicJsonObject(d); 

Then you can use

 string jsonStr = "{\"B\":\"2\",\"A\":\"1\"}"; JavaScriptSerializer jss = new JavaScriptSerializer(); jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() }); dynamic json = jss.Deserialize(jsonStr, typeof(object)) as dynamic; string result = new JavaScriptSerializer().Serialize((json as DynamicJsonObject).Dictionary); 

And result will have the expected result.

+2
source share

I know this may be a bit late, but if you need to sort the internal data arrays (I just needed this):

 static void Sort(JObject jObj) { var props = jObj.Properties().ToList(); foreach (var prop in props) { prop.Remove(); } foreach (var prop in props.OrderBy(p => p.Name)) { jObj.Add(prop); if (prop.Value is JObject) Sort((JObject)prop.Value); if (prop.Value is JArray) { Int32 iCount = prop.Value.Count(); for (Int32 iIterator = 0; iIterator < iCount; iIterator++) if (prop.Value[iIterator] is JObject) Sort((JObject)prop.Value[iIterator]); } } } 

Hooray!

0
source share

All Articles