What is JSON ? (simple explanation)

JSON stands for JavaScript Object Notation. To understand how JSON works we need to take a look at how the browser and the server communicate. When exchanging data between a browser and a server the data that is being transferred is of type text.

JSON is text-based format representing structured data based on JavaScript object syntax. Altho JSON is often closely related to javaScript, mostly all modern programing languages support encoding and decoding JSON data.

Now let’s take a look at an example of a json data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"name": "John",
"age": 21,
"male": true,
"likesColors": [
"blue",
"black",
"red"
],
"spaceShip": {
"color": "red",
"type": "cargo"
},
"education": null
}

As we can see here we have an object with a collection of key and value pairs so in this case the keys will be name, age, male, likesColors, spaceShip, education. As we see from this example JSON only supports a minimum number of value types such as: strings, numbers, booleans, arrays, objects, and null. So when we send this data to the browser in order to access it with JavaScript we need to decode this string using a method called JSON.decode(obj) and pass in in the object we want to decode.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
name: "John",
age: 21,
male: true,
likesColors: [
"blue",
"black",
"red"
],
spaceShip: {
color: "red",
type: "cargo"
},
education: null
}

As you can see the apostrophes from the key of the object are now gone and we have a real object that we can use and work with. And when we want to send data back to the server we use the method JSON.stringify(obj);

This was a brief introduction to JSON and a basic explanation of what it is and how to use it. Hope i made this easy to understand and very short. If you have any questions please feel free to ask below.
Cya in the next one.

Share