using System.Collections.Generic;
using Newtonsoft.Json.Linq;
public static void Main()
""entity_name"": ""Walters Multi-Member SIPP trust"",
""xpt_external_system_ref"": """",
""client_adviser_secondary"": 602,
""client_adviser_service"": 602,
""block"": ""Kintyre House"",
""street_name"": ""205 West George Street"",
""postcode"": ""G2 2LW"",
""allow_associates"": false,
""allow_multiple_account_associates"": false
""first_name"": ""Prabhu"",
""marital_status"": ""15"",
""resident_status"": ""Permanent Resident"",
""salutation"": ""Prabhu"",
""selected_retirement_date"": """",
""protected_allowance"": ""Individual Protection"",
""street_name"": ""Baker Street"",
""postcode"": ""W1U 7AL"",
""allow_associates"": false,
""allow_multiple_account_associates"": false
""country_code"": ""Brazil"",
""value"": ""9885801109"",
""allow_associates"": false,
""allow_multiple_account_associates"": false
""value"": ""masterek.org"",
""allow_associates"": false,
""allow_multiple_account_associates"": false
""value"": ""prabhu@gmail.com"",
""allow_associates"": false,
""allow_multiple_account_associates"": false
""allow_associates"": false,
""allow_multiple_account_associates"": false
""country_code"": ""India"",
""value"": ""9999999999"",
""allow_associates"": false,
""allow_multiple_account_associates"": false
JObject jObj = JObject.Parse(json);
var flattened = jObj.Flatten();
JObject unflattened = flattened.Unflatten();
WriteLine("Original JObject is equal to unflattened dictionary: " + JToken.DeepEquals(jObj, unflattened));
var flattenedWithoutEmpty = jObj.Flatten(false);
WriteLine("Original unflattened JObject without empty properties:\r\n" + flattenedWithoutEmpty.Unflatten().ToString());
WriteLine("Original flattened JObject:\r\n" + ToDebugString(flattened));
WriteLine("Original flattened JObject without empty properties:\r\n" + ToDebugString(flattenedWithoutEmpty));
WriteLine("Original JObject & unflattened dic are equal: " + JToken.DeepEquals(unflattened, jObj));
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}";