- In this video, we're going to discuss JSON,
or JavaScript Object Notation.
This is something that you need to know for the exam.
So make sure that you watch this video
and that you understand JSON.
(upbeat dance music)`
Okay, so let's start with why.
You've probably seen one of these before.
Are you a human or robot?
Now the reason we have captchas
is because machines struggle with unformatted data.
If the data isn't clear,
machines will struggle to interpret the data.
JSON is a way to format the data.
It's also a way to send data from one machine to another.
We, as humans, can interpret data quite easily,
especially if it's slightly off.
So as an example, if you type show version
on a Cisco IOS device, so let's say classic IOS,
the output will be different to a Cisco IOS XE device,
which is different to a Cisco Nexus device.
The output will vary slightly.
Now that's not a problem for us as humans.
We can make adjustments for the slight changes
in the output of the data, but machines struggle with that.
So we want data that is consistently formatted properly
so that machines can communicate properly.
As an example, if I SSH to a switch using Python,
so I connect to the switch using Python
and type show IP route or show version.
If I'm not using a JSON format,
so the output is not formatted in JSON format,
or another format such as XML or YAML,
it's difficult to write code
that can interpret the data properly.
Now Cisco, once again, have made things easier using pyATS.
So if you use pyATS rather than,
say Netmiko, to connect it directly
to a device using a Python script,
the output comes back in a format
that's easy to interpret using a Python script.
But if you're using pure Netmiko
or pure Telnet and you're simply
typing show version and you're trying to
pass that output, it's very difficult
to write good Python code to pass the
output, number one, and secondly,
to handle a situation when the output changes.
So you upgrade the router, as an example,
and the output formatting is different.
Your Python code will struggle to interpret that.
So, for machine to machine communication
it makes a sense to use standard output,
and it makes sense to have a format
that's easy to interpret, especially for machines.
So to help us with JSON formatting,
let's start with a Tesla.
Okay, I'm gonna ask you some questions
about this Tesla.
Well, first question is who's the manufacturer?
That would be Tesla.
What type of model is this?
Model X.
Notice how I've formatted this data.
Manufacturer: Tesla.
This is known as a key, and this is known as a value.
So we've got a key value pair.
What is the model?
It's Model X.
Color white.
Fuel electric.
Miles per gallon city 99.
Miles per gallon highway 93.
I've simply pulled this information off Google,
hopefully it's correct.
But don't get hung up about the data.
Look at the format of the data.
Again, we have what's called a key
separated by a colon, and then we have a value.
You need to understand JSON formatting.
That's an example of JSON formatting.
It actually looks like this.
We have to use double inverted commas.
So it would be something like
firstname colon, then David, as an example.
So JSON data is written as name value pairs.
So going back here, I said key.
It could be also referred to as name.
So, name value pair.
Another example is lastname.
And a value.
Name value pair consists of a field
in double quotes followed by a colon,
followed by a value.
Make sure you understand that format.
Now, for the exam there are two JSON data
types that you need to know, objects and arrays.
Let's start with an object.
So, a JSON object would look something like this.
Notice we've got curly braces.
Data is surrounded by curly braces
and it's an unordered collection of key value pairs.
So here's our key.
Firstname: David, lastname: Bombal.
Notice separated by a comma.
Make sure that you know the format.
So, curly braces surrounds the data.
We've got key value.
The keys and values use double quotes not single quotes.
Each key value pair is separated by a comma,
except the last one.
Trailing commas must not be used.
Now it's going to be easier to look at
it in this format, easier to read.
Spaces are not important with JSON.
So notice the curly braces
First key first value.
Second key second value, separated by a comma.
Comma third key, third value.
No comma.
So if you put a comma here that would be a problem.
Make sure, once again, that you recognize
this format for the exam.
So, just to summarize, a JSON object
is an unordered collection of key value pairs.
In other words, this is not ordered.
It could be in any order, doesn't matter.
Surrounded by curly braces, once again.
Key and value pairs are separated by a colon,
spaces don't matter.
But make sure that the last one doesn't have a comma.
Use double quotes not single quotes.
That's all very good and well, me showing
you the stuff with PowerPoint, but let me
show you this practically.
You'll be able to download this PowerPoint presentation
so that you have access to the details.
But I want to show you this practically
because it's going to be a lot easier to
demonstrate this on a live Cisco device
rather than just showing you the stuff
with PowerPoint.
Now the great thing is that DevNet
have a Nexus always on sandbox.
You can simply SSH to the sandbox
and you'll be able to try commands,
which I'm going to show you right now.
Now on a Mac I'm going to use this command,
but on Windows you could use Putty,
as an example, to connect.
Okay, so I'm going to copy this.
Open up a terminal.
I'll make this big to make sure that it's clear.
And I'm going to paste that command in.
So basically I'm SSHing to port 8181,
using this username and this is the DNS name of the router.
I have to put my password in, which is admin_1234!
or bang if you prefer.
So, there's the password.
There's the user name.
This is the port.
This is the protocol we need to use,
and this is the domain name.
Okay, it's complaining about my password,
let's try that again.
Okay, so I've successfully logged in to
this Nexus box, hosted by Cisco DevNet.
Cisco DevNet have made things so much easier
because you can practice this stuff,
you can try things using their free labs.
Now on a Windows computer you could use Putty,
and SSH to port 8181.
So SSH.
Hostname is sbx-nxos-mgmt.cisco.com.
And I can click open.
You have to accept the public key,
so I'm going to say yes.
Login as admin.
Admin_1234! or bang.
And there you go.
I've logged in.
So, show version.
This command shows me that this is a Nexus device.
We can see various information about the device.
We can see it's a 9000v Chassis, as an example.
So I'm going to jump back to my Mac and do this on my Mac.
Show version on the Mac will show
the same kind of information.
Now we can use standard commands like show int vlan 100.
And that shows us standard information
like we would get on any other type of device.
So we can see, as an example,
DEMO PLEASE DON'T TOUCH IP address is this.
But what we could do is we can send that to JSON.
So, let's just use JSON, and we get to
the JSON information like that,
but it's not very easy to read.
So let's use json-pretty.
So json-pretty.
There's our information.
So show int vlan 100.
We see the JSON formatting.
Notice the curly braces.
We've got information about this interface Vlan100.
Important to note, key, value,
separated by a colon then a comma,
key, value again, comma at the end,
double inverted commas.
Very important.
The last entry doesn't have a comma at the end.
Data is contained within curly braces.
So this data has these curly braces.
TABLE_interface has these curly braces.
And then we've got the all encompassing curly braces,.
So that's an example, practically, of JSON
formatting versus show interface vlan 100.
Standard command.
This is a format that we as humans would understand,
but this is a format that a machine
will understand much better.
So just remember, we want to have standard formatting
for machines.
As an example, sh ip int brief here
gives us a list of interfaces on the device.
That's human readable format.
But if we say json-pretty,
this gives us the information in JSON formatting.
Sh ip int brief | json-pretty.
Notice there's a big difference here,
we have the square brackets.
So ROW_intf, square bracket, scrolling down.
Now the link may be quite slow here.
My internet connection to the DevNet site is quite slow,
but notice here is the next square bracket.
Here we've got data within curly braces.
Again, notice no comma at the end here,
but this has curly braces and a comma at the end.
The data within here is key value comma.
Now the reason why we have square brackets
is because we are using what's called an array.
In this slide I've kind of summarized it.
Notice the curly braces.
Here we've got square brackets.
And inside here we've got an ordered list of values.
The reason why we want this is
because we've got multiple interfaces.
So we've got interface Vlan100.
And then here we've got interface Vlan200,
and information about those interfaces.
So rather than just showing information
about one interface, so a single object,
we've now got an array of interfaces
that we're looking at.
So an array in JSON is an ordered list of values.
Uses square brackets.
It can store all kinds of data.
So, it could store things such as strings.
A string is like a sentence, hello, or a word, hello.
Whereas a number is something like 1 or 2 or 2.123.
Values must be separated by commas, once again.
So, notice once again that an object
doesn't have those square brackets.
This is the output for show version | json-pretty.
Important here, comma.
Comma.
No comma.
We've got the key value pair separated by a colon.
Make sure that you know this formatting.
I have said that enough times now I think.
So here's an example, once again,
of show interface Vlan100.
Again you can download this PowerPoint
slides so that you have all this information.
I've given you a few examples of that.
But again, it's much easier to see this practically.
And I'm going to take this to the next level now
by showing you how to run Python on this Nexus box
and do stuff in Python, and then I'm going to show you
how to access the box remotely using a Python script
that I'm going to run on my local computer
and then pull out similar JSON information.
(upbeat dance music)