using System.Collections.Generic;
using Newtonsoft.Json.Linq;
private static readonly string json = @"{ 'array': [ 1, 2, 3 ], 'property.with.period.in.path': true, 'number': 123, 'object': { 'a': 'b', 'c': 'd', 'e': 'f' }, 'date': '2014-01-01T23:28:56.782Z', 'string': 'Hello World', 'null': null, 'emptyString': '', 'emptyObject': {}, 'emptyArray': [] }";
public static void Main()
JObject jObj = JObject.Parse(json);
var flattened = jObj.Flatten();
WriteLine("Flattened JObject:\r\n" + ToDebugString(flattened));
JObject unflattened = flattened.Unflatten();
WriteLine("Unflattened JObject:\r\n" + ToDebugString(unflattened));
WriteLine("JObject is equal to unflattened dictionary: " + JToken.DeepEquals(jObj, unflattened));
var date = flattened.Get<DateTime>("date");
WriteLine("\"date\" property: " + date);
flattened.Set("date", date.AddDays(5));
WriteLine("Updated \"date\" property: " + flattened.Get<DateTime>("date"));
var flattenedWithoutEmpty = jObj.Flatten(includeNullAndEmptyValues: false);
WriteLine("Flattened JObject without empty properties:\r\n" + ToDebugString(flattenedWithoutEmpty));
WriteLine("Unflattened JObject without empty properties:\r\n" + flattenedWithoutEmpty.Unflatten().ToString());
public static Action<object> WriteLine = (msg) => Console.WriteLine("\r\n" + msg.ToString() + "\r\n");
public static string ToDebugString<TKey, TValue>(IDictionary<TKey, TValue> dictionary)
return "{\r\n\t" + string.Join(",\r\n\t", dictionary.Select(kv => kv.Key + " = " + kv.Value).ToArray()) + "\r\n}";