MySQL vs. JSON - Why?

MysqlJson

Mysql Problem Overview


I'm designing a little web-app/game. What would be better: MySQL tables or json files? They both store information. They can both be parsed by PHP. What are the advantages/disadvantages?

This is what I mean:

username | password
-------------------
seefour  | abc123

vs.

{
  "username":"seefour",
  "password":"abc123"
}

EDIT: Wow, It's been just 3 years since I asked this question and it's surprising to see how much I've matured since when I asked this question. From a future me to the past me, this is why the two don't work. (In case anybody naive like me at the time can refer to this)

I used to think the two were interchangeable because they were both pretty much ways of storing information, although storing and using JSON files was easier to me at the time. Databases are separate pieces of software that make retrieving data much faster and don't end up being bloated over time. Also, carrying all the data in one or two files makes it dangerously easy to end up getting your data stolen or lost, where as a database is much more secure with those. Fundamentally, data shouldn't be part of your code; it should be a separate thing that your code works with.

Also, you'll learn about hashing and salting a couple of years down the line, so don't store passwords in plain text!

Mysql Solutions


Solution 1 - Mysql

To be really blunt about, MySQL is a database while JSON is not, so the correct answer is MySQL, without hesitation. JSON is just a language, and barely even that. JSON was never designed to handle anything like concurrent connections or any sort of data manipulation, since its own function is to represent data, not to manage it.

So go with MySQL for storing the data. Then you should use some programming language to read that database, and send that information as JSON, rather than actually storing anything in JSON.

If you store the data in files, whether in JSON format or anything else, you will have all sorts of problems that people have stopped worrying about since databases started being used for the same thing. Size limitations, locks, name it. It's good enough when you have one user, but the moment you add more of them, you'll start solving so many problems that you would probably end up by writing an entire database engine just to handle the files for you, while all along you could have simply used an actual database.

Solution 2 - Mysql

MySQL will be preferable for many reasons, not the least of which being you do not want your web server process to have write access to the filesystem (except for possibly logging) because that is an easy way to get exploited.

Also, the MySQL team has put a lot of engineering effort into things such as replication, concurrent access to data, ACID compliance, and data integrity.

Imagine if, for instance, you add a new field that is required in whatever data structure you are storing. If you store in JSON files, you will have to have some process that opens each file, adds the field, then saves it. Compare this to the difficulty of using ALTER TABLE with a DEFAULT value for the field. (A bit of a contrived example, but how many hacks do you want to leave in your codebase for dealing with old data?)

Solution 3 - Mysql

The 2 are not really comparable.

MySQL stores data in a database or actually is a database. JSON stores data in a format to be passed to and from the server to the client. Javascript/jquery can use JSON as data objects, but they only exist on the client side for the life of the page.

So if you wanted to store data as JSON(not recommended) you'd probably have to store them as text files to save the data.

You should store data in a database. Use functions to convert it to JSON format, then pass it to the webpage for javascript to consume and present to the user.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionavinashbotView Question on Stackoverflow
Solution 1 - MysqlTeekinView Answer on Stackoverflow
Solution 2 - MysqlMelenethView Answer on Stackoverflow
Solution 3 - MysqlYogurt The WiseView Answer on Stackoverflow