title: How can i remove all unnecessary values in JSON with regex?
body: <p>How can i find all unnecessary values in the following JSON example with regex to minimize string json data.</p>
  
  <p><strong>Unnecessary Value conditions:</strong></p>
  
  <ul>
  <li><strong>key</strong>=any and <strong>value</strong>= 0  </li>
  <li><strong>key</strong>=any and <strong>value</strong>= 0.0 </li>
  <li><strong>key</strong>=any and <strong>value</strong>= "0000-00-00" </li>
  <li><strong>key</strong>=any and <strong>value</strong>= ""  (empty string)</li>
  <li><strong>key</strong>=any and <strong>value</strong>= []  (empty array)</li>
  <li><strong>key</strong>=any and <strong>value</strong>= {}  (empty object)</li>
  <li><strong>key</strong>=ends with "ID" and <strong>value</strong>= all chars of value are 0 ("000" or "00000" or "0") </li>
  </ul>
  
  <p><strong>json example:</strong></p>
  
  <pre><code>{ "a_3":0, "a_1":"", "a_4":0.0, "a_2":"0000-00-00", "a_5":[], "a_6":{}, "a_7ID":"0000000000","b_12ID":"0000000001", "a_8ID":"000000","b_1":0.1,"b_2":2, "b_3":1.0, "b_4":"0", "b_5":"sf" }
  </code></pre>
  
  <p>(I named unnecessary keys starting with "a_" above)</p>
  
  <p><strong>My Work:</strong> <a href="https://regex101.com/r/kMe3Zt/4" rel="nofollow noreferrer">https://regex101.com/r/kMe3Zt/4 </a></p>
  
  <p>Regex :<code>(\"[^"]*\"\s*:(((\[\])|(\"\")|"0000-00-00"|"0000000000"|(\{\}))|(0.0))+,?)</code></p>
  
  <p>c# code:</p>
  
  <pre><code>  public static string clearJSON(string data)
      {
          var regexx = @"(\""[^""]*\""\s*:((\[\])|\""\""|0|""0000-00-00""|""0000000000""|(\{\}))+,?)";
          var result = "";
          do
          {
              result = Regex.Replace(data, regexx, string.Empty);
          } while (Regex.IsMatch(result, regexx));
          return result.Replace(",}", "}").Replace(",]", "]");
      }
  </code></pre>
  
  <p>Thanks.</p>