Alright, so we know what serialization is all about. Let's look at our first
file format and this is going to be JSON.
So what JSON actually stands for is JavaScript object notation.
Now the temptation might be if you've heard of a programming language known as
well JavaScript.
You might suspect that JSON is only for JavaScript, but this is actually not
the case.
Pretty much JSON was derived from the way in which we create objects within
JavaScript.
But don't let that confuse you thinking that this is a JavaScript only concept. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
But don't let that confuse you thinking that this is a JavaScript only concept.
As we said in the previous video, utilizing things like JSON, XML, YAML.
One of the benefits here is language neutrality. This is exactly what we're
getting here.
Now the first thing to state about JSON is that it is fairly human readable.
This is obviously an advantage when we happen to store our data.
If we can read it, then it does make it a little bit easier to work with.
It's not quite as human readable as say, for example, YAML.
But generally speaking, if you happen to look at a JSON document, you can get a
good idea
about the data that has actually been stored and how it's been stored. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
about the data that has actually been stored and how it's been stored.
Now quite honestly, there is a massive focus on JSON in the world of automation
and programmability.
The reason why is it with respect to things like REST APIs.
Now we actually haven't talked about REST APIs quite just yet, but we'll
definitely look
at it very, very soon.
When we happen to interact with REST APIs, the most common way that we're going
to get
your data back is in this JSON format, so it's really, really key that we
understand how
it operates because it's absolutely everywhere.
It's so, so popular.
Now, just as an aside, if you happen to recall the way Python happens to create WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now, just as an aside, if you happen to recall the way Python happens to create
particular
objects, say for example, when we want to create a Python dictionary, we can
use things
like curly braces.
If we want to create lists, we can use square brackets.
There is a lot of similarities between JSON and regular Python objects.
So you know the way in Python dictionaries, you specify a particular key and
then you get
a corresponding value.
We have the same type of concept within JSON.
Now let's just focus in on the actual value for a moment.
What type of data can actually be stored as a value in JSON? WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
What type of data can actually be stored as a value in JSON?
Well, many of the common types that you would expect, so say for example, we
want to store
a string value.
This is absolutely a valid value within JSON.
And this is the data type you would use for words or sentences, descriptions,
that type
of thing.
The next data type we can have is a number.
Now really similar to say, for example, integers in Python, the difference
between a number
and a string is very, very important.
Say, for example, you want to perform some type of mathematical calculation or
mathematical
evaluation. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
evaluation.
You want to make sure that your numerical value is not wrapped in quotations.
So say, for example, this here is a string, whereas this here is a number.
So say, for example, you were storing some information about your VLANs in the
JSON documents.
And you wanted to parse the information about the actual VLAN number.
Now, if you were using Python, let's maybe say you wanted to write some
conditional logic
to run a particular check against this value.
So what kind of check?
Well, let's maybe say we want to ensure that our VLAN range, or rather the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Well, let's maybe say we want to ensure that our VLAN range, or rather the
number being
used, is within a particular range.
And we wanted to specify that that valid range was between 1 and 4,000, and I
believe
it's 95 as a VLAN range.
A little bit rusty on my VLANs, but I do believe that's the case.
Now, if we happen to just define a value, say, for example, VLAN 19 as a string
, then we
actually cannot perform this mathematical evaluation.
So this is why it's important to use the correct type within our value, because
if we happen
to load this JSON information into our Python script, thus converting it to a WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to load this JSON information into our Python script, thus converting it to a
Python object,
we want to make sure it's going to be interpreted as in the medical value and
integer value.
OK, so we have strings, we have numbers, what other data types are available to
us?
Well, we can utilize a billion value, and this just means either a true or
false value,
which is once again, really good for utilizing conditional logic.
This ultimately here is a binary value.
So say, for example, you've got an interface configured.
Well, is that interface in the upstate where you could have that be true?
Or if it's in the downstate, you could have that value be false. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Or if it's in the downstate, you could have that value be false.
There is no 45% on or 30% off.
It's either on or it's off.
The Boolean data type allows us to specify this.
Now, the next value we can have is one known as the null data type.
Pretty much all null means is as it sounds, it is an empty data type that is
just there
to represent nothing but rather than just leaving a blank space.
Instead, we can explicitly represent this with this null value.
Now, the next data type that we can have as a value is one known as an object. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now, the next data type that we can have as a value is one known as an object.
Now, this really is just another JSON object with them.
Pretty much if we want to have some type of nested JSON, then we can just layer
objects
with an object.
Now, the way we happen to define an object within JSON is pretty much how we
would define
a dictionary in Python.
It's going to be utilizing these curly braces here, which are really bad.
They're drawing them immediately.
But that is what it's supposed to be, a curly brace.
And similarly, we also have the other data type known as an array.
This is a kind of analogous to a list object in Python once again we use our WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
This is a kind of analogous to a list object in Python once again we use our
square brackets.
And just like a list, it is used to denote multiple values.
Pretty much so we can have multiple instances of the same type of keys.
Now, with respect to the actual keys itself that we use, these keys absolutely
without
doubt must be string values.
So we can't use a number for a key.
We can't use a billion for a key, that type of thing.
They must be a string.
And this is very, very important.
When we define these keys as strings, we cannot just be using single quotation
marks. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
marks.
OK, you must use double quotations for this to be valid.
So let me just go and show you some examples of working with JSON.
So what we'll do here is I'll just say touch and let's just recall this VLAN
data and
give it a dot JSON extension.
So let's open this up here.
All right, so all I've done is paste in this information.
What we have here is some JSON data representing our VLAN configurations.
And like I say, looking at this, this might look suspiciously like Python.
And quite honestly, there are a lot of similarities, but notice that this is WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And quite honestly, there are a lot of similarities, but notice that this is
fairly human readable.
You can probably interpret what this is.
So look at the very top.
We have our curly brace, the note in a JSON object.
And here is the corresponding closing curly brace right here down the bottom.
Now inside of that, we have our opening key, which is VLAN's notice.
It is a string value as are all the keys and they're all in case within double
quotation
marks.
We just zoom in so you can see this a little bit better.
Okay, so let's go and say I changed the key to remove the quotation marks.
Look at this right here.
See this little red squiggly line. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
See this little red squiggly line.
This is actually telling us we have an error.
If I hover over it, property keys must be double quoted.
So even if I happen to do this and this, we still have an error.
We really have to make sure that we're utilizing these double quotes for these
keys.
Now inside here, we actually have an array object very similar to what we call
a list
in Python.
This allows us to specify multiple instances of the same object.
So inside this array object right here, look at the blue color braces inside, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So inside this array object right here, look at the blue color braces inside,
we actually
have this other JSON object.
In fact, we've actually got another JSON object within this as well.
Okay, now the crucial thing to note here, why do we actually need this array
object?
Well, this is because we have another rule within JSON is that we cannot
actually duplicate
keys.
Now, just to be clear here, I'm not talking about never having the same key in
the
entire JSON document.
This is obviously not true because look at this right here, we have a name key
right here
and then a name key right here.
I'm talking about the same keys within the same JSON objects.
So say, for example, in this object right here, if I added a new key right here WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So say, for example, in this object right here, if I added a new key right here
, such as
name and called this maybe HR for human resources, now we're going to have a
duplication and this
is now going to be a problem, whereas when we happen to have a JSON object like
this
and we put it within an array, we're actually allowing multiple JSON objects of
the same
structure.
So realistically, if we have a type of data that we want to effectively model
right here,
such as a name, a number and SVI, that type of thing and we want to have more
than one
of these objects, then we want to wrap the entire thing within an array. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
of these objects, then we want to wrap the entire thing within an array.
This is why we do it.
To say, for example, I remove the array, delete there, as well as a closing
brace right
here.
If I happen to save this, look what actually happens down here, we're now
beginning to
see these errors appear.
This is no longer valid JSON.
So let me just put this back and we're now back to working JSON.
So as we can see, we have this data structure here, VLANs, and inside of this,
what we have
are three instances.
The instance in position zero is the first VLAN, which we can see right here,
the mark
into one with number 10. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
into one with number 10.
Inside of that, we have a switched virtual interface, another JSON object.
We can open this up, expand this, see the IP address as well as a subnet mask.
If we go back, let's go to the second object right here.
Again, we can see the name is sales number 20.
We have a JSON object within the SVI, we can see this as the IP address and sub
net mask.
And then finally, we can also see the third instance of the JSON object right
here.
Once again, finance number is 30 SVI, here is information right here.
So actually, as in this nice hierarchical structure, as you can see right there WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So actually, as in this nice hierarchical structure, as you can see right there
, now one
thing to note, if you're coming from YAML, you know YAML is super human
readable.
It's very, very nice on the eyes, but it's also very sensitive to indentation
and white
space.
It might be somewhat of a relief for you to learn that when you're working with
JSON,
white space and indentation, absolutely does not matter a jot.
Really has no effect at all, so what you can do is if you so wish, you can just
go and
make this look all weird and wonderful by messing with the actual indentation, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
make this look all weird and wonderful by messing with the actual indentation,
add a
lot of white space here.
So if I was going to make this look really, really ugly, like so, let me zoom
out a little
bit.
So you can see this all looks a little bit dodgy, but if I happen to take all
this information
right here, copy it.
If I search for something called JSONLint, which is an online JSON validator,
if I go here
and I paste in, well, this rather ugly looking data, if I validate it, it does
actually say
the JSON is indeed valid.
See that?
And what the Lunter actually does here are automatically reformats it to make
it look a
little bit neater. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
little bit neater.
But the point is, is that the data here as presented is still valid and
understandable,
it's still intelligible.
Now one big thing to really note about JSON, and this is something that is
really, really
worth remembering, okay, let me just save this here to get the reformatting.
When it comes to our JSON objects, they're going to be separated via commas,
but, and this
is the thing to remember, we are not allowed to have any trailing commas.
So that means the last object must not actually have a comma.
So look at a straight here, okay, within our array, we have three JSON objects. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So look at a straight here, okay, within our array, we have three JSON objects.
Just look at the blue curly braces.
Here is the first one, and then we have a little comma right here, and then we
have the
next object right here, and then right next to it we have another comma, and
then we come
to the last JSON object within this array.
So let me just zoom down a little bit, so let me just scroll down a little bit,
and here
it is right here, see this.
Now you'll notice because this is the last object in the array, we actually do
not have a
comma right after this, because that would be a trailing comma and that would
invalidate
the JSON.
Similarly, we also have this top level JSON object, which basically encaps WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Similarly, we also have this top level JSON object, which basically encaps
ulates absolutely
everything, and just zoom it a little bit.
Here it is right here.
And notice this one here also does not have a trailing comma at the end.
So if I wanted to make the data invalid, what I could do is get down to the
last one right
here, add a trailing comma.
As we can see here, I have my red squiggly line.
If I go over it, it says here, this is a trailing comma, so I must get rid of
it, so remove
here.
If I try it right here, and hit a comma, again, this is a red squiggly line.
Once again, we have invalidated our JSON. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Once again, we have invalidated our JSON.
So really just understand that with respect to JSON, whilst it was derived from
JavaScript
object notation, it is still absolutely language neutral.
It can look very, very similar to Python, but it does have very particular
strict rules about
the type of data we can save and how we structure the corresponding keys as
well as making
sure that when we happen to have our JSON objects, we do not want to have any
trailing
commas at the end.
And also with respect to JSON, unlike things like YAML, white space and indent
ation does not
matter at all.