0

I have a JSON file that I want to be able to amend in C#. I want to be able to delete a set of data.

[
{
    "ID": "ID0000001",
    "Name": "ABCD",
    "Std": "4"
},
{
    "ID": "ID5335355",
    "Name": "JKLM",
    "Std": "6"
},
{
    "ID": "ID53534535",
    "Name": "XYZ",
    "Std": "12"
}
]

how to remove the one id,name and std. like this :

{
    "ID": "ID0000001",
    "Name": "ABCD",
    "Std": "4"
}

after delete this json file like this :

[    
{
    "ID": "ID5335355",
    "Name": "JKLM",
    "Std": "6"
},
{
    "ID": "ID53534535",
    "Name": "XYZ",
    "Std": "12"
}
]

I will need to perform one by one delete operations on the Json file. The Json file could contain thousands of results and I really need the most performant way possible.

Any help greatly appreciated.

3
  • I think you need to convert JSON to list of objects, remove and serialize it back to JSON Commented Jun 23, 2015 at 11:01
  • Can these one-by-one operations be batched, or do they have to be performed in isolation? Commented Jun 23, 2015 at 11:19
  • Maybe you should look into BSON format. It will be quicker to parse it Commented Jun 23, 2015 at 11:37

1 Answer 1

2

You can use Json.NET to deserialize the string into objects. Then use Linq to select the wanted items, and then use Json.NET again to get the JSON string.

public class Item
{
   public int ID {get; set;}
   public string Name{get;set;}
   public int Std {get; set;}
}

var items = JsonConvert.DeserializeObject<List<Item>>(JsonString);

var newJsonString = JsonConvert.SerializeObject(items.Where(i => i.ID != "ID0000001"));
Sign up to request clarification or add additional context in comments.

2 Comments

I am not able to understand where is the code for deletion, please suggest.
items.Where(i => i.ID != "ID0000001") returns all Items except for the one with the id of "ID0000001".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.