title: Recursive JSON parsing using JSON.NET
body: <p>I have the following JSON object returned from a JSON differ:</p>
  
  <pre><code>{
      "lastName": ["Bab", "Beb"],
      "middleName": ["Cg", "seeg"],
      "contact":
      {
          "emailAddress": ["bab@example.com", "beb@example.com"],
          "addresses":
          [
              {
                  "state": ["AL", "AZ"]
              },
              {
                  "state": ["TN", "MO"]
              }
          ]
      }
  }
  </code></pre>
  
  <p>I need a list of changes in the following fashion.</p>
  
  <pre><code>lastName/new:Bab/old:Beb
  middleName/new:Cg/old:seeg
  contact.emailAddress/new:bab@example.com/old:beb@example.com
  contact.addresses[0].state/new:AL/old:AZ
  contact.addresses[1].state/new:TN/old:MO
  </code></pre>
  
  <p>So I wrote this ugly program using a bit of recursion.</p>
  
  <pre><code>private static IEnumerable&lt;DocumentProperty&gt; ParseJObject(JObject node)
  {
      HashSet&lt;DocumentProperty&gt; documentProperties = new HashSet&lt;DocumentProperty&gt;();
      DocumentProperty documentProperty = new DocumentProperty();
  
      foreach (KeyValuePair&lt;string, JToken&gt; sub in node)
      {
          if (sub.Value.Type == JTokenType.Array)
          {
              // unnamed nodes which contain nested objects 
              if (sub.Value.First.Type == JTokenType.Object)
              {
                  foreach (var innerNode in sub.Value.Children())
                  {
                      documentProperties.UnionWith(ParseJObject((JObject)innerNode));
                  }
              }
  
              documentProperty = CreateDocumentProperty(sub.Value);
          }
          else if (sub.Value.Type == JTokenType.Object)
          {
              documentProperties.UnionWith(ParseJObject((JObject)sub.Value));
          }
  
          documentProperties.Add(documentProperty);
      }
  
      return documentProperties;
  }
  </code></pre>
  
  <p>It worked except that it is getting me some extra output.</p>
  
  <pre><code>lastName/new:Bab/old:Beb
  middleName/new:Cg/old:seeg
  contact.emailAddress/new:bab@example.com/old:beb@example.com
  contact.addresses[0].state/new:AL/old:AZ
  contact.addresses[1].state/new:TN/old:MO
  contact.addresses/new:{                &lt;-----------------------------Extra here.
    "state": [
      "AL",
      "AZ"
    ]
  }/old:{
    "state": [
      "TN",
      "MO"
    ]
  }
  </code></pre>
  
  <p>I suspect that it is due to how I have my recursion setup. Can you immediately make out what is wrong here?</p>
  
  <p>Definition for <code>CreateDocumentProperty</code></p>
  
  <pre><code>private static DocumentProperty CreateDocumentProperty(JToken subValue) =&gt; new DocumentProperty()
  {
      PropertyName = subValue.Path,
      New = subValue[0].ToString(),
      Old = subValue[1].ToString()
  };
  </code></pre>
  
  <p>Main method:</p>
  
  <pre><code>static void Main()
  {
      JToken jToken = JToken.Parse("{\"lastName\":[\"Bab\",\"Beb\"],\"middleName\":[\"Cg\",\"seeg\"],\"contact\":{\"emailAddress\":[\"bab@example.com\",\"beb@example.com\"],\"addresses\":[{\"state\":[\"AL\",\"AZ\"]},{\"state\":[\"TN\",\"MO\"]}],}}");
  
      JObject inner = jToken.Value&lt;JObject&gt;();
      IEnumerable&lt;DocumentProperty&gt; data = ParseJObject(inner);
  
      foreach (var item in data) Console.WriteLine(item);
  }
  </code></pre>