title: JSON: Keys and Field Names: Dynamically Parse
body: <p>I have a JSON string</p>
  <pre><code>    {&quot;key&quot;: &quot;2021-01-01 22:59:59&quot;, &quot;data&quot;: {&quot;field1&quot;: &quot;newvalue1&quot;, &quot;field2&quot;: &quot;newvalue2&quot;}}
  </code></pre>
  <p>I have to morph this json into the following:</p>
  <pre><code>    {
          &quot;field1&quot;: {&quot;before&quot;: &quot;oldValue1&quot;, &quot;new&quot;: &quot;newvalue1&quot;},
          &quot;field2&quot;: {&quot;before&quot;: &quot;oldValue2&quot;, &quot;new&quot;: &quot;newvalue2&quot;}
      }
  </code></pre>
  <p>The problem is that field1 and field2 are not always present. THe original JSON is dynamically generated by a DevExtreme DataGrid's Updates to a row. This is how far I got:</p>
  <p>EDIT:
  The below code works</p>
  <pre><code>    var outputJSON = {};
      var changes = {&quot;field1&quot;: &quot;newvalue1&quot;, &quot;field2&quot;: &quot;newvalue2&quot;};
      for(var i = 0; i &lt; Object.keys(changes).length; i++){
          var keyName = Object.keys(changes)[i];
          outputJSON[keyName] = {};
  
          outputJSON[keyName][&quot;before&quot;] = Object.values(changes)[0];
          outputJSON[keyName][&quot;after&quot;] = Object.values(changes)[1];
      }
      console.log(JSON.stringify(outputJSON));
  </code></pre>
  <p>I think this is closer to what I need.</p>