I like languages. I don’t know about you, but’s fascinating to me how there are so many different languages (like how did they all come about?). I believe being able to speak multiple languages, or to understand and be understood by multiple cultures is a superpower. Do you remember the story of the Tower of Babel and how the work seemed to go smoothly and easily as long as everyone understood themselves? Remember that as soon as the languages became divided, chaos ensued and the work was destroyed? That goes to show how powerful and orderly it can be when everyone understands themselves. And that’s why JSON is a superpower.
JSON? Do you mean the rapper? (yes, there’s an actual rapper with the name Json)
JSON, short for JavaScript Object Notation, is like the universal format that different programming language can understand. Many languages, like Python and JavaScript, have some kind of method of parsing JSON data, and it just makes the whole process of building web applications smooth. But it wasn’t always like that you know.
The dark, dark days
Back in the days, everyone used (not me, I wasn’t even born yet!) a contraption called Extensible Markup Language (XML) to deal with web data. It was the popular way to do it. The “markup” in the name should already tell you that this contraption was related to HTML somehow. They were probably siblings. Using the HTML sibling that didn’t turn out well (you know, the one that the parents don’t like to talk about) was a pain. In fact, it was painful just to look at it. Want to store a simple list of books? With XML,you’d end up with something like this:
<books>
<book>
<title>A Spell Of Good Things</title>
<author>Ayobami Adebayo</author>
<rating>4.5</rating>
</book>
<book>
<title>The Secret Lives Of Baba Segi's Wives</title>
<author>Lola Shoneyin</author>
<rating>4.8</rating>
</book>
</books>
Compare that with storing the same list in JSON format:
[
{
"title": "A Spell Of Good Things",
"author": "Ayobami Adebayo",
"rating": 4.5
},
{
"title": "The Secret Lives Of Baba Segi's Wives",
"author": "Lola Shoneyin",
"rating": 4.8
}
]
Doesn’t it look scary to even attempt to parse the XML data? And OMG THE VERBOSITY! Want to send a few pieces of information over an API? XML would happily quadruple the size of your data. And to add to this mess, parsing XML was another level of complexity. You couldn’t just take an API and manipulate it like a normal person would.
Cool! Tell me more about this JSON that’s not a rapper
If you’re already familiar with how dictionaries look like in Python, or how objects look like in JavaScript, then this should not be a problem to you. JSON data is structured in key-value pairs like you saw in the example above. Each key has a value, which can be a string, number, boolean, array, object, or even null. JSON objects can also be nested, meaning a key’s value can be another JSON object or an array of objects.
Most programming languages have built-in methods to parse JSON data. Like I said, it’s universal.
With JSON, I can fetch data from a website using Python, and someone else can use the same data in JavaScript, and there’d be no compatibility issues! And yes, JSON doesn't support every possible data type (like functions or complex objects), but it's flexible enough for almost any web application.
JSON is cool and all but so what? Why should I care?
Imagine you’re building a book recommendation website, and you would want to rank the books from highest rated to the ones that have their 1-star reviews; would you go out and get a billion people rating 10 billion books to get the data you would need? Of course not! You’d be dead before you start writing code for your MVP. But then, there are sites that have collected all this data and have it stored somewhere, and have made it free (or not) to access through their API which would most likely be in JSON format (except they are like the guy that declared “the horse is here to stay” when the car was invented). And you can integrate their data to build your book recommendation website without having any data of your own, regardless of whatever language you want to utilise!
What if you want to build another thing but you don’t have a back-end or database to work with? Well, here comes JSON to the rescue! If your programming language can support JSON, it would have methods to help you read from and write to a JSON file. You can store your data by writing to the JSON file, and then read it when you want to retrieve it. Yes, I know JSON can never replace having a good back-end and database (even Superman has Kryptonite you know), but it works great for local storage, small applications, or even static data for a frontend project. No fancy backend setup; just a simple file that holds your data in a structured, readable format!
And don’t get me started on all the config files that work with JSON format. That’s a whole different story you know.
I hope you can feel the power of JSON, which is much more than a simple format to store data, but the glue that holds the entire structure of any web application together. Now you go out there and jsonify the world!