1

I have a Json file which holds data like:

[
  {
    "applicationName": "ABC",
    "machineName": "XYZ",
    "OrderNumber": "N46464646"
  },
  {
    "applicationName": "DEF",
    "machineName": "XYZ",
    "OrderNumber": "D46364636"
  },
  {
    "applicationName": "ABC",
    "machineName": "XYZ",
    "OrderNumber": "D34343434"
  }
]

I want to remove a set of object from the above data based on Order Number for eg: If I give order number "D46364636", it should remove-

{
    "applicationName": "DEF",
    "machineName": "XYZ",
    "OrderNumber": "D46364636"
}

I have tried it doing like this-

JsonDataList = new List<JsonItem>();
                string input = "";
                string inputApplicationName = "";
                string inputMachineName = "";
                string inputOrderNum = "";
                while (input != "q")
                {
                    Console.WriteLine("Press 'd' to Delete item");
                    Console.WriteLine("Press 'q' to Quit Program");
                    Console.WriteLine("Press Command:");
                    input = Console.ReadLine();
                    switch (input)
                    {
                        case "d":
                            Console.WriteLine("Enter Order Number to remove:");
                            inputOrderNum = Console.ReadLine();
                            var js = File.ReadAllText(@"C:\Users\ab54253\Documents\visual studio 2010\Projects\JSONExample\JSONExample\JsonData\AutomatedJson.json");
                            var result=JsonConvert.DeserializeObject<List<JsonData>>(js);
                            foreach(var item in result)
                            {
                                if(item.OrderNumber==inputOrderNum)
                                {
                                    JsonDataList.Remove(new JsonItem(item.applicationName, item.machineName, item.OrderNumber));
                                }
                            }
                            break;
                        case "q":
                            Console.WriteLine("Quit Program");
                            break;
                        default:
                            Console.WriteLine("Incorrect Command, Try Again");
                            break;
                    }
                }
                Console.WriteLine("Rewriting AutomatedJson.json");
                string JD = JsonConvert.SerializeObject(JsonDataList, Newtonsoft.Json.Formatting.Indented);
                File.WriteAllText(@"C:\Users\ab54253\Documents\visual studio 2010\Projects\JSONExample\JSONExample\JsonData\AutomatedJson.json", JD);
                Console.ReadLine();

I am able to check with the condition and I used to remove data by using:

if(item.OrderNumber==inputOrderNum)
{
  JsonDataList.Remove(new JsonItem(item.applicationName, item.machineName, item.OrderNumber));
}

By using above piece of code it is removing all the data from the JSON file instead of removing required data.Please guide me.

1
  • You haven't populated JsonDataList. So, that Remove line is not removing anything, because the list is already empty. You can change the var result = ... line to JsonDataList = .... However, Rajnik Patel's anwer is more succinct. Commented Nov 28, 2016 at 14:20

1 Answer 1

1

You can use 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 string applicationName {get; set;}
       public string machineName {get;set;}
       public string OrderNumber {get; set;}
    }

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

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

2 Comments

Thanks for the reply Rajnik. But could you please tell me what will be the type of i and why we are using this like this inside where condition? I am actually new to JSON so it will be help for me to understand.
It's the syntax of a Lambda expression. The argument to pass (parameter) is on the left of the => and the method(s) that use it are on the right hand side of it.

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.