About JSON

 

JSON is a modern alternative for structuring data. As opposed to other "old fashion" methods such as XML, JSON is compact, lightweight and easy to read (once you get the hang of it).

JSON uses punctuation to denote "lists", "sets" and "key-value pairs".

- [ ] Square brackets denote lists.
- { } Curly brackets denote sets.
- , Commas separate things.
- : Colons denote key-value pairs.
- " " Double quotes are used to define keys and human-readable words.

These are the only characters needed and used to define complex data structures.

Yup, just these 7 core characters can go a long way.

Key-Value Pair

A key-value pair is exactly what the name implies: A key that has a value. If we have some data, we need to explain what it is. The key is used to explain the meaning of the value.

Hence, the key and the value are a pair.

A key-valye pair is like underwear. You need the left leg hole and the right leg hole in order to have one pair of underwear.

The colon is used to separate the key from the value. The colon signifies that the key and the value are directly related -- that they are a pair.

Example

name : bob

In the example above "name" is the key and "bob" is the value.

The example above is not valid JSON because the words meed to be wrapped in quotes. We're just trying to explain concepts here.

 

Quotes

Quotes server two purposes:

1. They are used to define keys.
2. They clump human-readable words together.

Since keys can contain any character, including spaces and dashes, we use quotes to prevent odd-ball characters (non-alpha-numeric characters) from spilling into the structure and/or polluting the structure with characters that are illegal.

An illegal character is any character that is not one of those 7 core characters used to create JSON.

So anything that is not one of those 7 punctuation characters must be wrapped with quotes.***

Building on our original example:

"name" : "bob"

Now we have a valid key-value pair because both the key and the value are wrapped with quotes.

*** When numbers or booleans (true or false) are used as values (in a key-value pair) they do not need quotes -- this is because the colon character is magical. Since the colon ties a key to a value, the right-hand side of the colon get's evaluated as having some kind of tangible value. Numbers and true and false are special-case values that can't possible have any weirdo characters within them. Hence they don't need to be wrapped within quotes to prevent spillage of weirdo characters.

Commas

Commas are used to separate things.

Building on our example:

"name" : "bob",
"age" : 24

NOTE: Ah ha! the number 24 doesn't need quotes!

In our example above, the comma is used to separate 2 key-value pairs. Without the comma, the two key-value pairs could potentially mash up against each other and looks something like:

"name" : "bob""age" : 24 (wrong)

... and that just doesn't feel right.

"name" : "bob", "age" : 24

... aaah that's better.

We only need to put commas in-between things we are trying to separate. For example, we dont' need to put a comma after the last "thing" because there's nothing after the last thing, so there's no need to put a comma there!

Example

"name" : "bob",
"age"  : 24,
"city" : "San Diego" <-- no comma needed!

 

So we use a comma to isolate "things" from each other.

{ Sets }

Sets are used to clump together key-value pairs.

Sets are often refered to as "objects" because the term "object" is a common term in many programming languages. They both refer to the same concept.

Clumping together one or more key-value pairs allows us to package up our key-value pair information into a single entity --a "set" or "object".

To create a set we simply wrap one or more key-value pairs with curly brackets like so:

{
	"name" : "bob",
	"age"  : 24,
	"city" : "San Diego"
}

Here's a nice small set that only has one key-value pair packaged up as a set:

{ "name" : "bob" }

You'll notice that there aren't any comma's in there because there's nothing we need to separate. Eveything is encapsulated within the curly braces.

However, what if we had a few sets? Yup, we'd then have to separate each set with a comma as:

{ "name" : "bob" },
{ "name" : "sally" },
{ "name" : "frank" }

... that's because comma's are used to separate things, be it key-value pairs or sets, that's what comma's do.

Here we have largers sets for comparison

{
	"name" : "bob",
	"age"  : 24,
	"city" : "San Diego"
}, <-- our little comma
{
	"name" : "sally",
	"age"  : 30,
	"city" : "New York"
}, <-- our little comma
{
	"name" : "frank",
	"age"  : 45,
	"city" : "Seattle"
} <-- no comma needed

 

[ Lists ]

Lists are used to to clump together anything (accept key-value pairs).

Lists are similar in nature to sets, however, sets are specifically used to clump together key-value pairs, whereas lists are used for everything else.

Lists are often refered to as "arrays" because the term "array" is a common term in many programming languages. They both refer to the same concept. The word array is scarey, list is not.

We wrap the stuff we want to clump together with square brackets. For example, we can clump together a bunch of numbers as:

[
	8,
	13,
	21,
	34,
	55
]

 

The comma rules apply to lists as well. The comma's separate each item in the list. If we didn't have the commas, the numbers would all smash up against each other and be indistinguishable.

We can clump together a few sets as:

[
	{
		"name" : "bob",
		"age"  : 24,
		"city" : "San Diego"
	},
	{
		"name" : "sally",
		"age"  : 30,
		"city" : "New York"
	},
	{
		"name" : "frank",
		"age"  : 45,
		"city" : "Seattle"
	}
]

We can also mix-and-match stuff in our list as:

[
	42,
	"bob",
	{
		"name" : "sally",
		"age"  : 30,
		"city" : "New York"
	},
	89,
	{"name" : "frank"}
]

... just as long as everything is separated with a comma.

Newlines, Tabs & Spaces

Newlines, tabs and spaces can be used wherever you want. These chracters have no special meaning and are used specifically for humans.

And that's the foundation for how JSON works.

Read, re-read, repeat.