Skip to content

Latest commit

 

History

History
50 lines (41 loc) · 1.31 KB

ModifyJson.md

File metadata and controls

50 lines (41 loc) · 1.31 KB

Modifying JSON

This sample loads JSON, modifies Argon.JObject and Argon.JArray instances and then writes the JSON back out again.

var json = """
    {
      'channel': {
        'title': 'Star Wars',
        'link': 'http://www.starwars.com',
        'description': 'Star Wars blog.',
        'obsolete': 'Obsolete value',
        'item': []
      }
    }
    """;

var rss = JObject.Parse(json);

var channel = (JObject) rss["channel"];

channel["title"] = ((string) channel["title"]).ToUpper();
channel["description"] = ((string) channel["description"]).ToUpper();

channel.Property("obsolete").Remove();

channel.Property("description").AddAfterSelf(new JProperty("new", "New value"));

var item = (JArray) channel["item"];
item.Add("Item 1");
item.Add("Item 2");

Console.WriteLine(rss.ToString());
// {
//   "channel": {
//     "title": "STAR WARS",
//     "link": "http://www.starwars.com",
//     "description": "STAR WARS BLOG.",
//     "new": "New value",
//     "item": [
//       "Item 1",
//       "Item 2"
//     ]
//   }
// }

snippet source | anchor