using System.Threading.Tasks;
using System.Text.RegularExpressions;
string uri = "https://coderbyte.com/api/challenges/json/json-cleaning";
string response = GetAsync(uri).Result;
string filtered = FilterJsonResponse("{\"name\":{\"first\":\"Daniel\",\"middle\":\"\",\"last\":\"Smith\"},\"age\":45,\"DOB\":\"-\"}");
Console.WriteLine(filtered);
private static async Task<string> GetAsync(string uri)
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
return await reader.ReadToEndAsync();
private static string FilterJsonResponse(string jsonString)
string originalEmptyValues = "(\"\"|\"[-]\"|\"N\\/A\")";
string emptyPlaceholder = "#empty#";
string emptyKeyValues = "(\"[a-zA-Z]+\":#empty#,|,\"[a-zA-Z]+\":#empty#)";
string emptyArrayValues = "(#empty#,|,#empty#)";
string result = Regex.Replace(jsonString, originalEmptyValues, emptyPlaceholder);
result = Regex.Replace(result, emptyKeyValues, "");
result = Regex.Replace(result, emptyArrayValues, "");