January 10th, 2020

Earlier this month, I wrote about MySQL with JSON support being "fucking tight".

This might be the first time I can recall quoting myself, but fair warning... I kind of like it.

Allow me to elaborate.

When you process form data through HTTP's POST protocol for example, to get it into a MySQL database previously, you would have to create a field for each, or serialize the array and store that if you want to cram it into 1 field.

The problem with serialising data is that it sucks. The way it formats the data is that it specifies how many characters are available which makes it rigid. For example, if you store strings such as email and you want to change the field directly through PHPMyAdmin, then you would have to do the following replace

string(26): "thisemail@doesntexist.com"

with

string(25): "thisemail@doesntexist.co"

It is a pain because you have to actually change the number to change the variable which means you have to count characters somehow which is dumb.

But... a better array exists that can be represented as TEXT in the database. Enter JSON.

To convert to an associative array to a JSON string, you just have to

<?php $json = json_encode($array); ?>

To extract to an associative array

<?php $array = json_decode($json, 1); ?>

This is a very powerful way to move data around. It also minimises processing.

The alternative more common way to store information is to setup field by field. But when you do that, you the amount of conversion you have to do is orders of magnitude higher. So putting everything in a JSON field can easily cut the dev time in half or better.

But in the past, the problem with storing arrays in TEXT MySQL fields was that you could not query into them. This limits how you report on things.

But the addition of JSON support to MySQL solves this. Now you can query inside the JSON field.

MySQL does not support Indexing inside JSON tables. And I don't think it needs to. Exposing keys you would join on still makes good sense from a development standpoint to easily pick out data through PhpMyAdmin. That is why I say add the fields you want to join on outside of the JSON (even if it makes them redundant) for usability and rapid development. Then you index these fields and your reporting will be blistering fast.

So... when does this come in handy? It works amazing on data where you want the user to be able to enter anything at all. No restrictions on type checking or boundaries.

I have also been able to save security settings by generating configurations through more authenticated users. Such that an authenticated user can save a configuration of permissions they are allowed to grant to a user.

I was able to come up with a use case where a customer is taken through a series of steps. Each step they have to save information. Since all the steps behaved the same way (save POST data to JSON field) I was able to able to do all the processing on all 9 steps and load the entire object with all 9 steps in their current state with 50 lines of code.

So all the dev focus is really on the UI/UX. It is working quite well.

Posted In:

ABOUT THE AUTHOR:
I am TSLA Long. Model 3 Owner. Brother of a Model 3 owner. Son of a Model S owner. I have reservations for Slate Roof and Cybertruck. I am a Tesla speculator and fanboy. I am not a financial advisor. Investing in anything comes with inherent risk. This is not financial advice.