C
C#2y ago
Owen

❔ Removing JSON data from JSON File

Hey, I am attempting to remove JSON data from a text file if required (I'm using it as a local database) This was my attempt:
Database? db = JsonConvert.DeserializeObject<Database>(File.ReadAllText("Files\\Database.json"));
foreach (var pendingOrder in db.PendingOrders)
{
var orderIndex = Program.PendingOrders.FindIndex(o => o.CustomerInfo.UserId == pendingOrder.CustomerInfo.UserId && o.CustomerInfo.ChannelId == pendingOrder.CustomerInfo.ChannelId);
if (orderIndex < 0)
{
db.PendingOrders.Remove(pendingOrder);
}
}
Database? db = JsonConvert.DeserializeObject<Database>(File.ReadAllText("Files\\Database.json"));
foreach (var pendingOrder in db.PendingOrders)
{
var orderIndex = Program.PendingOrders.FindIndex(o => o.CustomerInfo.UserId == pendingOrder.CustomerInfo.UserId && o.CustomerInfo.ChannelId == pendingOrder.CustomerInfo.ChannelId);
if (orderIndex < 0)
{
db.PendingOrders.Remove(pendingOrder);
}
}
Obviously, this doesn't work and I receive this error
System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.'
System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.'
I know this is because I am directly modifying the collection by removing the pending order - so I am hopefully looking for a better way of doing this. Thanks a lot.
11 Replies
Owen
Owen2y ago
Note: I am using Newtonsoft.Json
ero
ero2y ago
any reason why you're using newtonsoft? System.Text.Json is a lot faster (especially in .net 7)
Owen
Owen2y ago
My whole codebase uses it and it was all I was familiar with. Am using .NET 6
ero
ero2y ago
yeah stj is still faster in net6
Owen
Owen2y ago
I will likely look to convert in the future - but for now it's probably too much to change over Have quite a lot of JSON processing in this project
ero
ero2y ago
for (int i = db.PendingOrders.Count - 1; i >= 0; i--)
{
var pendingOrder = db.PendingOrders[i];

int oi =
Program.PendingOrders
.FindIndex(o =>
o.CustomerInfo.UserId == pendingOrder.CustomerInfo.UserId
&& o.CustomerInfo.ChannelId == pendingOrder.CustomerInfo.ChannelId);

if (oi == -1)
db.PendingOrders.RemoveAt(i);
}
for (int i = db.PendingOrders.Count - 1; i >= 0; i--)
{
var pendingOrder = db.PendingOrders[i];

int oi =
Program.PendingOrders
.FindIndex(o =>
o.CustomerInfo.UserId == pendingOrder.CustomerInfo.UserId
&& o.CustomerInfo.ChannelId == pendingOrder.CustomerInfo.ChannelId);

if (oi == -1)
db.PendingOrders.RemoveAt(i);
}
Owen
Owen2y ago
Appreciate this, I will test this after dinner and report back here! Hey, so this is exactly how i've implemented this. Upon running the function, it seems to run successfully however the JSON file still retains the data.
Owen
Owen2y ago
Owen
Owen2y ago
Owen
Owen2y ago
This is right at the end of the void Resolved! Was fixed by changing the db.PendingOrders = null to a new List Thanks for your help
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.