using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public static class JsonExtensions
public static TJToken RemoveEmptyArrayProperties<TJToken>(this TJToken root) where TJToken : JToken
var container = root as JContainer;
var query = container.DescendantsAndSelf()
.Where(p => p.Value is JArray && ((JArray)p.Value).Count == 0);
foreach (var property in query.ToList())
public string bar { get; set; }
public Foo foo { get; set; }
public static void Test()
foreach (var jsonString in GetJson())
var root = JObject.Parse(jsonString)
.RemoveEmptyArrayProperties()
var newJson = JsonConvert.SerializeObject(root);
Console.WriteLine(newJson);
static string[] GetJson()
public static void Main()
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public class AssertionFailedException : System.Exception
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
public static class Assert
public static void IsTrue(bool value)
public static void IsTrue(bool value, string message)
throw new AssertionFailedException(message ?? "failed");