using Newtonsoft.Json.Linq;
public static void Main()
JToken token = JsonHelper.RemoveEmptyChildren(JToken.Parse(json));
Console.WriteLine(token.ToString(Formatting.Indented));
public static class JsonHelper
public static JToken RemoveEmptyChildren(JToken token)
if (token.Type == JTokenType.Object)
JObject copy = new JObject();
foreach (JProperty prop in token.Children<JProperty>())
JToken child = prop.Value;
child = RemoveEmptyChildren(child);
copy.Add(prop.Name, child);
else if (token.Type == JTokenType.Array)
JArray copy = new JArray();
foreach (JToken item in token.Children())
child = RemoveEmptyChildren(child);
public static bool IsEmpty(JToken token)
return (token.Type == JTokenType.Null);