Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I best utilize Json.NET to modify parts of an existing JSON object?

Tags:

json

c#

json.net

Example:

I have the following JSON object.

{"currentVersion" : "10.0", 
  "folders" : [], 
  "services" : [
    {"name" : "nyc", "type" : "MapServer"}, 
    {"name" : "philly", "type" : "MapServer"}
  ]
}

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.

{"name" : "nyc", "type" : "MapServer"}

The end result is an object that looks like this:

{"currentVersion" : "10.0", 
  "folders" : [], 
  "services" : [
    {"name" : "philly", "type" : "MapServer"}
  ]
}

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.

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.

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();

How can I best utilize Json.NET to modify parts of an existing JSON object?

like image 814
Ryan Taylor Avatar asked Jul 08 '10 17:07

Ryan Taylor


1 Answers

I have yet to determine an alternate way to modify an existing JSON than what was provided in the original question. If a better way surfaces I'll gladly accept that as the correct answer.

In the meantime, the solution that I have implemented is to replace the parts of the JSON object I need modified with newly created objects. An example is shown below.

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();
like image 131
Ryan Taylor Avatar answered Sep 22 '22 08:09

Ryan Taylor