title: How can I best utilize Json.NET to modify parts of an existing JSON object?
body: <p><strong>Example:</strong></p>
  
  <p>I have the following JSON object.</p>
  
  <pre><code>{"currentVersion" : "10.0", 
    "folders" : [], 
    "services" : [
      {"name" : "nyc", "type" : "MapServer"}, 
      {"name" : "philly", "type" : "MapServer"}
    ]
  }
  </code></pre>
  
  <p>I want to be able to remove one or more items from the services array based on the value of the name attribute.  For instance, I want to search for name="nyc" and remove the following object from the array.</p>
  
  <pre><code>{"name" : "nyc", "type" : "MapServer"}
  </code></pre>
  
  <p>The end result is an object that looks like this:</p>
  
  <pre><code>{"currentVersion" : "10.0", 
    "folders" : [], 
    "services" : [
      {"name" : "philly", "type" : "MapServer"}
    ]
  }
  </code></pre>
  
  <p>I have been able to create new JSON object fairly easily and I can read existing ones.  However, I am having difficultly determining the best way to modify an existing JSON object. Particularly as it relates to querying for specific objects within the JSON object.</p>
  
  <p>One method that I have uncovered (specific to this example) is to rebuild portions of the JSON that I want modified and replace the tokens as necessary.  However, I have to think there is a better way. This code is shown below.</p>
  
  <pre><code>string json = @"{""currentVersion"" : ""10.0"", 
                      ""folders"" : [], 
                      ""services"" : [
                          {""name"" : ""nyc"", ""type"" : ""MapServer""}, 
                          {""name"" : ""philly"", ""type"" : ""MapServer""}
                      ]
                  }";
  
  string[] keepList = new string[] { "nyc" };
  
  JObject o = JObject.Parse(json);
  JArray services = (JArray)o["services"];
  JArray newServices = new JArray();
  
  foreach (JToken service in services)
  {
      foreach (string keeper in keepList)
      {
          if ((string)service["name"] == keeper)
          {
              newServices.Add(service);
              break;
          }
      }
  }
  
  services.Replace(newServices);
  
  string output = o.ToString();
  </code></pre>
  
  <p>How can I best utilize Json.NET to modify parts of an existing JSON object?</p>