JSON The Language of the Internet

Jay-Sahn the “data format”

What is JSON, pronounced Jay-Sahn? It’s what we call a “data format”. It’s a predefined “standard” used to describe how data is interpreted. JSON itself is a way of organizing data and it does this via a “key-value” pairing, because computers are far better at understanding this format compared to written human text. There are other ways to format data which include XML, CSV, and HTML, however the majority of web APIs utilize JSON as the method of communication.

To break down what a “key-value” pairing let’s take a look at an imaginary banana farm API. This API returns data about someone’s banana farm in the form of JSON below:

{
  "banana_color": “yellow”,
  "banana_count": 40,
  "banana_trees": 3,
  "are_banana_trees_ripe": true,
  “ripe_banana_tree_ids”: [1, 2, 3], 
  “dead_banana_trees”: null
}

A “key-value” pairing in JSON is a format which consists of a left hand side, a colon, and a right hand side. The side left of the colon is the “key” and the side right of the colon is the “value”. The left hand side, or the “key”, is used to describe something about the right hand side, for example the “key-value” pairing: "banana_color": “yellow”, the key "banana_color" is used to tell us that the right hand side denotes a color. The actual unit of data, the “value”,"yellow", is located on the right hand side and is the concrete piece of data.

The braces in JSON denote an “object”, an “object” is a collection of data inside JSON, starting with an open brace { and ending with a closing brace }. You can think of it just like a basket, a basket may have many different things inside of it but that doesn’t change what the basket is, it’s still a basket no matter what you put into it. A JSON “object” is a basket of data in different forms. Just like a basket may have other containers in it, for example say your basket has your lunch box in it, JSON can have other objects located inside each other starting and ending with their own curly braces.

JSON also has different “types”, types are like different collections of “things.” Like ice-cream you can have different flavors: vanilla, chocolate, strawberry; these are all “types” of ice-cream, well in JSON we can also have different flavours or “types” of data. The specific types of data in JSON are: Numbers, Strings, Boolean, Null, Objects, and Arrays.

Let’s take a look at all the “values” in the JSON from our banana API from start of the JSON object to the end, we have: “yellow”, 40, 3, true, [1, 2, 3], and null. Each one of these values has a type. Let’s describe these types in the table below:

Value Type
“yellow” String
40 Number
3 Number
true Boolean
[1, 2, 3] Array
null Null

Without going into too much detail let me briefly explain to you each of the component types:

Strings are data which are surrounded by quotes and a series of letters, numerals, symbols and punctuation marks. They can be useful to denote human readable data, for example the color yellow.

Numbers are just that actual numbers and nothing else, strings can have numbers inside their quotes but “40” is not the same as 40. The string “40” is not a number whereas 40 is a number.

Booleans are just true or false values. They get their special type in computer science because so much of what software engineers do boils down to yes or no, do this or do that, so much so we made it special.

Arrays are just lists, they can contain a list of things. If you’re confused as to how arrays are different than objects don’t fret, they are similiar except lists are ordered and sequential, meaning you have to go through each element in order to get a value one at a time, whereas with an object you can pick and choose the thing out of the “basket” right away.

Null is an interesting concept, it literally is what we use to denote empty or nothing. Null is nada, it’s void, it means that thing is nothing.

Annnnnd guess what? That’s about it, there’s really not a whole lot to JSON. As long as you understand the concept of a “key-value” store and that “values” can have different “types” that’s 90% of JSON. The other 10% is understanding that JSON can have any sort of “structure”. What do I mean by that? It means that JSON can be ordered however you want it for example:

You can add JSON objects inside of JSON objects and so on:

{
  "banana_farm_name": “yellow tubes”,
  "banana_farm_details": {
    "banana_color": “yellow”,
    "banana_count": 40,
    "banana_trees": 3,
    "are_banana_trees_ripe": true,
    “ripe_banana_tree_ids”: [1, 2, 3], 
    “dead_banana_trees”: null,
    "some_other_object": {}
  }
}

You can add JSON objects inside of JSON arrays.

{
  "banana_farms": [
    "yellow_tubes", 
    "banana_mango", 
    "the cool banana",
    {
      "some_other_farm_name": "mango_tree"
    }
  ]
}

You can add JSON arrays inside of JSON arrays and so on

{
  "banana_farms": [
    "yellow_tubes", 
    "banana_mango", 
    "the cool banana",
    [ "the_wacky_nested_banana_farm", "cool_breeze_associates", "big_tree_banana" ]
  ]
}

As long as there’s a key and a value which is a valid JSON type of: Number, String, Boolean, Null, JSON object, Array you can structure and create any arbitrary piece of JSON. It is literally limitless to how many different ways you can structure your data.

So there’s nothing to be scared off by understanding JSON you are now speaking the language of the internet! Now you too understand how APIs communicate and provide us useful data.

Related Posts