In the programming world, JSON is a popular data format to exchange information. What’s cool about this is, every programing language has agreed to support JSON format. Let’s say calling a movie database to find all the info about a specific movie.
Jason Voorhees
The server will return the movie data in this looking format:
{
"Title": "Friday the 13th",
"Year": "2009",
"Rated": "R",
"Released": "13 Feb 2009",
"Runtime": "97 min",
"Genre": "Horror, Mystery, Thriller",
"Director": "Marcus Nispel"
}
JSON is actually a JavaScript Object Notation. It’s invented by this cool guy:
Douglas Crockford
So let’s talk about JavaScript objects first.
You probably know about different types of variables like:
String name = "Taylor Swift";
int age = 100;
boolean isAwesome = true;
These are what we call primitive values that programming languages give you to start off. But what if you need a more complex set of custom data types?
For this we have objects.
I have heard so many explanations from many developers and teachers about what classes and objects are. But I figure out, they are nothing more than just a custom data type.
A Person
is a custom data type I created that has a name, age, hasKilled, and birthday information:
class Person {
String name;
int age;
boolean hasKilled;
String birthday
}
And this is how it will look like in JavaScript (JS has no types):
var jason = {
name : "Jason Voorhees",
age : 74,
hasKilled : true,
birthday: "June 13, 1946"
}
Okay, so that works well but the problem comes when sending this data over the internet to another application.
Enter JSON!
JSON is similar to JavaScript objects but here all the properties and values are enclosed in double-quotes. This makes them a string of text that can be sent across the internet:
{
"name":"Jason Voorhees",
"age":74,
"hasKilled":true,
"birthday":"June 13, 1946"
}
That’s just the start of JSON formatting. You can have objects inside these JSON objects:
{
"name":"Jason Voorhees",
"age":74,
"hasKilled":true,
"birthday":"June 13, 1946",
"info": {
"weapon": "Machete",
"location": "Camp Crystal Lake",
"classification": "Mass Murderer"
}
}
You can even have lists or arrays:
{
"name":"Jason Voorhees",
"age":74,
"hasKilled":true,
"birthday":"June 13, 1946",
"info": {
"weapon": "Machete",
"location": "Camp Crystal Lake",
"classification": "Mass Murderer"
},
"victims": ["Barry", "Claudette", "Bill", "Brenda"]
}
Now you can convert any object to JSON and vice versa.
In JavaScript, there is a global class JSON that has two methods for making and decoding them:
JSON.stringify()
Converts a standard object into JSONable text.
JSON.parse()
Does the opposite by taking a JSON string and outputting a JS object.
In Python there is a json package to do these things:
import json
json.loads()
loads the JSON text into an object.
json.dumps()
converts the python object into JSON text.
There are such methods or packages in every top programming language since JSON is universal.
After you convert your object to a JSON string, it will be a piece of text like this: {“name”:”Jason Voorhees”,”age”:74,”hasKilled”:true,”birthday”:”June 13, 1946",”info”:{“weapon”:”Machete”,”location”:”Camp Crystal Lake”,”classification”:”Mass Murderer”},”victims”:[“Barry”,”Claudette”,”Bill”,”Brenda”]}
Now you have your data to send over the internet!
Question: What other data formats out there?
Other than JSON, there are BSON, XML, YAML, binary, etc...
I hope you enjoyed this post. Do give it a clap if you do.