I am developing a light weight web app in ASP.NET with C# and I would like to store some data such as site analytics etc.
Instead of using a SQL server database could I use a JSON file instead? So I would open the JSON file and write into it when done. Could there be problems with simultaneous connection?
Is this standard/ common practice?
Any help will be highly appreciated.
2
Answers
It is probably a VERY bad idea. I would only do this if it was some text file to display a wee bit of information.
If you need updating? Then what happens if two users are on the site. The last person to update will over write the text based json file.
I suppose you could write out a "id" and json file for each record you edit, and that way a save of one record value would not over write others. But, putting more then one reocrd in a json file, and if users are to edit such values, then no, it will not work, since a simple save of that text json file will overwrite any other changes made by any other user who also happens to hit the same button.
It is VERY hard to find a web site hosting, even those for super cheap – less then $10 per month that does not include a database server system. And be it MySQL, SQL server, postgres sql and more? They all have free verisons you can use.
And I suppose you could consider the file based sqlLite. That would even be better. While it not considered thread safe, it can work if you only say have a a few users like 2-4 users working at the same time.
Because you have OH SO MANY choices here? The only reason to use a text based json file is if no other options exist – and boatloads of options exist in near all cases.
And if some database was not available? The I would simple include sqlLite in the project and use that.
I’m not super experienced but i would like to explain why i would never do that.
First of all everything (if you are brave enough) can be a database, as long as it is some kind of file that can give persistency to your data. Database are basically optimized one purpose software to store data. Mainly the problem in your solution is that you would need to read the file and load it in memory storing it as an object and then writing data to it like you would do with a static factory object and then serialise it back to JSON after you are don with it. Personally i don’t like this idea because i think is very prone to human error (like deleting the file by accident during mantainance) and it can be hard-er to debug if it starts accumulating a sizeable chunk of data. There are very lightweight data persistency solutions that implements SQLite that is a database for small applications, like Pocket Base. Since you are already developing a backend it would require you near to no effort to add a little table to store the analytics.