using Newtonsoft.Json.Linq;
using System.Collections.Generic;
string sampleJson = "{\"name\":{\"first\":\"Robert\",\"middle\":\"\",\"last\":\"Smith\"},\"age\":25,\"DOB\":\"-\"," +
"\"hobbies\":[\"running\",\"coding\",\"-\"],\"education\":{\"highschool\":\"N\\/A\",\"college\":\"Yale\"}}";
JObject inputJson = JObject.Parse(sampleJson);
ICollection<string> jsonKeys = GetAllKeys(sampleJson);
foreach (string key in jsonKeys)
RemoveJson(inputJson[key]!);
JObject cleanedJson = JObject.Parse(inputJson.ToString());
RemoveJsonFromRoot(inputJson, cleanedJson);
Console.WriteLine(cleanedJson);
ICollection<string> GetAllKeys(string json)
ICollection<string> keys = new List<string>();
var raw = JRaw.Parse(json);
foreach (JProperty prop in raw.OfType<JProperty>())
static void RemoveJson(JToken node)
foreach (var item in node)
string value = item.Last!.ToString();
if (value == "" || value == "N/A" || value == "-")
if (item.Value<string>() == "" || item.Value<string>() == "N/A" || item.Value<string>() == "-")
static void RemoveJsonFromRoot(JObject inputJson, JObject cleanedJson)
foreach (var jsonItem in from KeyValuePair<string, JToken> jsonItem in inputJson
where jsonItem.Value.ToString() == "" || jsonItem.Value.ToString() == "N/A" || jsonItem.Value.ToString() == "-"
cleanedJson.Remove(jsonItem.Key);