title: Update JArray in JObject JSON.NET
body: <p>I have two files one is a json another is xml and I need to merge both, I decided to transform the xml in json after concat/merge.</p>
  
  <pre><code>{
      "Level1": {
          "Level2": [
              {
                  "id": "Chart",
                  "Box": [
                      {
                          "id": "1",
                          "value": "10"
                      },
                       {
                          "id": "2",
                          "value": "20"
                      }
                  ]
              }
          ]
      }
  }
  </code></pre>
  
  <p>The second Json:</p>
  
  <pre><code>{
      "Level1": {
          "Level2": [
              {
                  "id": "NameApp",
                  "Box": [
                      {
                          "id": "2",
                          "value": "90"
                      },
                       {
                          "id": "3",
                          "value": "50"
                      }
                  ]
              }
          ]
      }
  }
  </code></pre>
  
  <p>OUTPUT :</p>
  
  <pre><code>{
      "Level1": {
          "Level2": [
              {
                  "id": "Chart",
                  "Box": [
                      {
                          "id": "1",
                          "value": "10"
                      },
                       {
                          "id": "2",
                          "value": "20"
                      }, {
                          "id": "2",
                          "value": "90"
                      },
                       {
                          "id": "3",
                          "value": "50"
                      }
                  ]
              }
          ]
      }
  }
  </code></pre>
  
  <p>XML code:</p>
  
  <pre><code> XmlDocument doc = new XmlDocument();
  
          doc.Load(pathXml);
          doc.RemoveChild(doc.FirstChild);
  
          string jsonTextXml = JsonConvert.SerializeXmlNode(doc);
  </code></pre>
  
  <p>JSON code:</p>
  
  <pre><code>using (StreamReader readerJson = new StreamReader(pathJson))
          {
              jsonTextJson = readerJson.ReadToEnd();
          }
  </code></pre>
  
  <p>Code for Merge:</p>
  
  <pre><code>JObject o1 = JObject.Parse(jsonTextJson);
  
  JObject o2 = JObject.Parse(jsonTextXml);
  
  JArray box1 = o1["Level1"]["Level2"]["Box"][0] as JArray;
  
  JArray box2 = o2["Level1"]["Level2"]["Box"][0] as JArray;
  
  box1 = new JArray(box1.Concat(box2));
  
  o1["Level1"]["Level2"]["Box"][0].Replace(box1);
  </code></pre>
  
  <p>When I wannt get the box1, I have this error: Object reference not set to an object instance.</p>
  
  <p>I tested with this another way..</p>
  
  <pre><code>JArray box1 = o1["Level1"]["Level2"][0]["Box"] as JArray;
  </code></pre>
  
  <p>What is wrong?</p>
  
  <p>Finally this was my solution SOLUTION:</p>
  
  <pre><code> public string joinJson(string jsonFinal, string jsonTemp)
      {
  
          JObject jsonMaster = JObject.Parse(jsonFinal);
  
          JObject jsonForMerge = JObject.Parse(jsonTemp);
  
          foreach (var element in jsonForMerge["Level1"]["Level2"])
          {
             string pathElement = element.Path;
             string component = pathElement.Split(new char[] { '.' }).Last();
  
             if (element.HasValues &amp;&amp; !component.Equals("id"))
              {
                  JArray contentTemp = jsonForMerge["Level1"]["Level2"][component] as JArray;
                  JArray contentFinal = jsonMaster["Level1"]["Level2"][0][component] as JArray;
  
                  contentFinal = new JArray(contentFinal.Concat(contentTemp));
                  jsonMaster["Level1"]["Level2"][0][component].Replace(contentFinal);
              }
          }
  
          return jsonMaster.ToString();
  
      }
  </code></pre>