Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 912687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:25:33+00:00 2026-05-15T17:25:33+00:00

Example: I have the following JSON object. {currentVersion : 10.0, folders : [], services

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:25:34+00:00Added an answer on May 15, 2026 at 5:25 pm

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 490k
  • Answers 490k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Even though faceting by range isn't supported in Solr 1.4.x,… May 16, 2026 at 9:55 am
  • Editorial Team
    Editorial Team added an answer The ntext data type is deprecated in favour of the… May 16, 2026 at 9:55 am
  • Editorial Team
    Editorial Team added an answer In general, you can do this my simply importing the… May 16, 2026 at 9:55 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a MySQL database called People which contains the following schema <id,name,foodchoice1,foodchoice2> .
Say for example I have the following string one two(three) (three) four five and
here is my problem I have the following array (for example) string[] arr =
I have the following jquery code: jQuery(function(){ jQuery(select#rooms).change(function(){ var options = ''; jQuery.getJSON(/admin/selection.php,{id: jQuery(this).val(),
I have a PHP script that does the following: Uses file_get_contents() to get content
I have a project that requires the following. Four arrays will be declared in
I'm looking at the following API: http://wiki.github.com/soundcloud/api/oembed-api The example they give is Call: http://soundcloud.com/oembed?url=http%3A//soundcloud.com/forss/flickermood&format=json
Possible Duplicate: When do you use the “this” keyword? If I have the following
I'm making a request to a JSON page using jQuery's $.getJSON method, and from
I wrote the following (part of a jQuery plugin) to insert a set of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.