title: how to add an empty array to a json payload?
body: <p><strong>Is it possible to add an array to a json payload?</strong></p>
  
  <p>For example, let's say this is my payload:</p>
  
  <pre><code>        string json = @"{
            'channel': {
              'title': 'Star Wars',
              'link': 'http://www.starwars.com',
              'description': 'Star Wars blog.',
              'obsolete': [{'something':'awesome'},{'a':'b'}],
              'item': [],
              'nested':[{'upgrademe':'please'}, {'upgrademe':'no'}]
            }
          }";
  </code></pre>
  
  <p>The result I'd like would be something like this:</p>
  
  <pre><code>        string json = @"{
            'channel': {
              'title': 'Star Wars',
              'link': 'http://www.starwars.com',
              'description': 'Star Wars blog.',
              'obsolete': [{'something':'awesome'},{'a':'b'}],
              'item': [],
              'nested':[{'upgrademe':'please', 'emptyArray':[]}, {'upgrademe':'no'}]
            }
          }";
  </code></pre>
  
  <p>For every <code>channel.nested</code> where <code>upgrademe</code> == <code>please</code> then I'd like to add an empty array like <code>[]</code>.</p>
  
  <p>before:</p>
  
  <p><code>'nested':[{'upgrademe':'please'}, {'upgrademe':'no'}]</code></p>
  
  <p>after:</p>
  
  <p><code>'nested':[{'upgrademe':'please', 'emptyArray':[]}, {'upgrademe':'no'}]</code></p>
  
  <p>My current implementation:</p>
  
  <pre><code>    public static JObject AddArray(this JObject jObject, string path, string key, string value)
      {
          var obs = jObject.SelectToken(path);
          if (obs == null)
          {
              return jObject;
          }
  
          foreach (var jsonObject in obs)
          {
              if (jsonObject.SelectToken(key).ToString().ToLower() != value.ToLower())
              {
                  continue;
              }
  
              jObject.SelectToken(path).Parent.AddAfterSelf(new JValue(new JProperty("emptyArray", new JArray())));
          }
  
          return jObject;
      }
  </code></pre>
  
  <p>I'm getting an exception on this line:</p>
  
  <p><a href="https://i.stack.imgur.com/WNqFr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WNqFr.png" alt="enter image description here"></a></p>
  
  <p>usage example:</p>
  
  <pre><code>        var result = jobject.AddArray("channel.nested", "upgrademe", "please");
  </code></pre>
  
  <p>the above says the following:</p>
  
  <blockquote>
    <p>find the object "channel.nested" and when upgrademe==please, add a sibbling node as an empty array</p>
  </blockquote>
  
  <p><strong>How do we conditionally add an array object to a json payload?</strong></p>