title: Using AutoMapper, Json.NET, or other library to update parent and children properties with changes that are in a json string
body: <p>I have a scenario where I am given a json string that ONLY contains the properties that have been modified for a given parent / children object by the user.  </p>
  
  <p>When given this string, I need to load the original object and update it with the changes that are in the json string.  </p>
  
  <p>Sample classes</p>
  
  <pre><code>public class ParentObject
  {
      public int ID { get; set; }
      public string Prop1 { get; set; }
      public string Prop2 { get; set; }
      public List&lt;ChildObject&gt; Children { get; set; }
  }
  
  public class ChildObject
  {
      public int ID { get; set; }
      public string PropA { get; set; }
      public string PropB { get; set; }
  }
  </code></pre>
  
  <p>Here is the "original" object.  </p>
  
  <pre><code>var originalObject = new ParentObject
  {
      ID = 1,
      Prop1 = "Prop 1 Original",
      Prop2 = "Prop 2 Original",
      Children = new List&lt;ChildObject&gt;() { new ChildObject {
              ID = 22,
              PropA = "Prop A Original"}
          }
  };
  </code></pre>
  
  <p>Here is the Json string with the "modified" object.  </p>
  
  <pre><code>string modifiedProperties = 
      @"[{
          ""ID"": 1,
          ""Prop1"": ""Prop 1 Changed"",
          ""Children"": [{
              ""ID"": 22,
              ""PropA"": ""Prop A Changed""
          }]
      }]";
  </code></pre>
  
  <p>I must then "update" the "original" object with the properties that changed.  </p>
  
  <p>I could use reflection to iterate through the properties of the "original" object and then compare those with the Json string BUT I was wonder if there is some other library like AutoMapper that can do this easier?  </p>
  
  <p>Newton Json.NET merge is looking promising.<br>
  <a href="https://www.newtonsoft.com/json/help/html/MergeJson.htm" rel="nofollow noreferrer">https://www.newtonsoft.com/json/help/html/MergeJson.htm</a></p>
  
  <p><strong>AutoMapper code -- Mostly working</strong><br>
  This code "ALMOST" works in that it does map the parent and child BUT it fails if the child has a property with the same name as the parent.<br>
  AKA the ID field is not mapping on the children.  </p>
  
  <pre><code>// Configure the mapping
  Mapper.Initialize(cfg =&gt; cfg.CreateMap&lt;ExpandoObject, ParentObject&gt;());
  
  // Convert the JSon into a dynamic list
  var sources = JsonConvert.DeserializeObject&lt;List&lt;dynamic&gt;&gt;(modifiedProperties);
  
  // Iterate the list of changes
  foreach (dynamic source in sources)
  {
      // Get the source ID to pull the record from the database
      var sourceID = Convert.ToInt32(source.ID);
  
      // Simulate a DB call to get the original object
      var destination = originalObject;
  
      // Map the objects together
      var realDestination = Mapper.Map(source, destination);
  
      // ** OUTPUT **
      // realDestination.ID = 1 &lt;-- THIS IS CORRECT
      // realDestination.Prop1 = "Prop 1 Changed" &lt;-- THIS IS CORRECT
      // realDestination.Prop2 = "Prop 2 Original" &lt;-- THIS IS CORRECT
      // realDestination.Children.FirstOrDefault().ID = 0 &lt;-- THIS SHOULD BE 22 NOT 0
      // realDestination.Children.FirstOrDefault().PropA = "Prop A Changed" &lt;-- THIS IS CORRECT
      // realDestination.Children.FirstOrDefault().PropB = null &lt;-- THIS IS CORRECT
  }
  </code></pre>
  
  <p>I am still working on the json merge option.<br>
  Any other suggestion are welcome.</p>