JSON, XML, & YAML
Synchronized Transcript
124 cues indexed • Click dialogue or words to jumpWelcome to Jeremy’s IT Lab. This is a free, complete course for the CCNA. If you like these videos, please subscribe to follow along with the series.
Also, please like and leave a comment, and share the video to help spread this free series of videos. Thanks for your help. In this video we will look at JSON, XML, and YAML.
These are known as data serialization languages, or data serialization formats. These allow us to format or structure data in a standardized way so it can be communicated between applications.
This is covered in exam topic 6.7, which says you must be able to interpret JSON encoded data. Note that it only mentions JSON, not XML or YAML.
So, the majority of this video will focus on JSON, but at the end I will take a bit of time to compare JSON to two other popular data serialization languages, XML and YAML.
Here’s what we’ll cover in this video. First I’ll briefly introduce data serialization and why we need it. Then I’ll cover JSON.
This will be the main part of this video, and we’ll focus on how to interpret basic JSON-encoded data. Then I’ll briefly introduce XML and YAML, so you can compare and contrast them with JSON.
However I won’t spend so much time on those two, since they’re not mentioned in the exam topics list. And make sure to watch until the end of the video for a bonus practice question from Boson Software’s ExSim, the best practice exams for the CCNA.
So, let’s take a look at what data serialization is. It is the process of converting data into a standardized format, or structure, that can be stored, for example in a file, or transmitted, for example over a network, and then be reconstructed later, perhaps
by a different application. Why is that useful? It allows the data to be communicated between applications in a way both applications can understand.
If one application is written in Python, and the other is written in Java, both languages store data differently so they need some standard format to send data between each other.
Data serialization languages, for example JSON, allow us to represent variables with text. Now, what is a variable? Here are some examples of variables.
Interface name, gigabitethernet1/1. Status, up. IP address, 192.168.1.1. Netmask, 255.255.255.0.
These are four variables. Basically, variables are containers that store values. For example ‘interface_name’ is a container, and it contains the value ‘gigabitethernet1/1’ inside of it.
‘Status’ is another container, and inside there is the value ‘up’. So, let’s see how data exchanges would work without the use of a standard data serialization format.
The client, for example an app trying to get information from an SDN controller, sends a GET message. The server, the controller, sends those variables directly to the client without converting them to a standard format like JSON.
However, the client doesn’t understand the data it received. The app and the controller are programs written in different languages and they store data differently, they can’t communicate directly.
So let’s see how a data serialization language such as JSON fixes that. The client sends a GET request. The requested variables are sent to the API, which converts them to a standard format, JSON, and sends them over the network to the client.
Because the variables are in a standard format that both the client and the server understand, the client is able to take those variables and convert them into its own native format.
Now, we haven’t looked at any details about how APIs work yet, I will cover them in the next video. So, this process might not be totally clear for you yet, but I’m sure you can see the benefit of having a standard data format like JSON to allow these two programs to share data with each
other. Now let’s take a look at JSON. JSON, which stands for JavaScript Object Notation, is an open standard file format and data interchange format, meaning it can be used to store data as files or to send data over a network, that uses human-readable text to store and transmit
data objects. So, although it’s used to exchange data between machines, so it must be easily machine-readable of course, it’s also quite human-readable.
It’s easy for us to read and understand. It is standardized in RFC 8259, which you can access at this URL. Or just do a google search for RFC 8259.
I recommend taking the time to read it. If you’re not used to reading technical documentation like this it might be a bit difficult to understand at first, but it’s the best source to really understand JSON.
Anyway, JSON was derived from JavaScript, but it is language-independent and many modern programming languages are able to generate and read JSON data.
Note that REST APIs often use JSON, which is why Cisco expects you to learn the basics of JSON for the CCNA exam. In JSON, whitespace is insignificant.
What does that mean? Well, I’ll demonstrate with examples later, but basically it means that spaces and linebreaks don’t matter. They don’t change the meaning of the data.
In JSON, there are four primitive data types. In the next slide I’ll demonstrate them, but they are string, number, boolean, and null. In addition, there are two structured data types, object and array.
Let’s look at these six data types and see how JSON data is actually formatted. First up, the primitive data types.
A string is a text value, and it is surrounded by double quotes. Here are some examples of strings. Hello. Notice that it is surrounded by double quotes.
This means it is a string, a series of alphanumeric characters. These are all also strings. So, remember that if it’s contained within double quotes, it’s a string, a simple text value.
Next, a number is simply a numeric value. It is not surrounded by quotes. So, 5, 100, or any other number. Note that the five in the string examples with the double quotes is a string, a text value, but the 5 down here without double quotes is a number, a numeric
value. They are different data types. Next, a boolean is a data type that has only two possible values. Those are true and false.
Note that they are written without quotes. In a networking setting, perhaps on an OSPF-enabled interface a variable named ‘passive’ would have the boolean value ‘true’ if the interface is passive, and if the interface isn’t passive the variable would have the boolean value ‘false’.
That’s just one example of how boolean values could be used. Note that in the string examples I included “true” with double quotes. That isn’t a boolean value, that’s just the word ‘true’, a series of letters.
However when written without double quotes, it is a boolean value. Finally, note that they are written all lower-case, no capital T in true or capital F in false.
The final primitive data type is null, which represents the intentional absence of any object value. It’s simply written as null, lower case n u l l, without quotes.
This simply means there is no value, it means nothing. Again, in the string examples I included “null” in double quotes. That is a string, because it is contained within double quotes.
It does not express a null value, it just means the word ‘null’. Okay, those are the four primitive JSON data types. String, number, boolean, and null.
Now let’s go on to the structured data types, there are two. First, an object is an unordered list of key-value pairs, basically variables.
Unordered means the order of the variables doesn’t matter. Objects are surrounded by curly brackets. The key, which is basically the name of the variable, the container, must be a string.
So, it must use double quotes. Then the value inside of that variable can be any valid JSON data type. A string, number, boolean, null, object, or array.
The key and the value are separated by a colon, and if there are multiple key-value pairs in the object, each pair is separated by a comma. Okay, let’s look at some examples to help you visualize it.
This is one JSON object. Note the curly bracket at the top indicating the start of the object, and the curly bracket closing it at the bottom.
Inside there are five key-value pairs. Before looking at this object in more detail, I want to show you this. This is the exact same object.
As I said before, in JSON whitespace is insignificant, it has no meaning. Of course, for humans the one on the left is easier to read, but computers don’t care either way.
So, I’ve colored the keys blue and the values red. All of this is one JSON object, but let’s look at the data type of each individual key and value.
First, the top one. “interface” in double quotes, that’s the key. Then notice a colon separating the key and the value, which is “gigabitethernet1/1” in double quotes.
So, the key and value are both strings, they both have double quotes. Remember that the key must be a string, but the value can be any valid JSON data type.
Notice that there is a comma after as well, because if there are multiple key-value pairs in an object, each pair is separated by a comma.
Okay, in the next key-value pair under it the key is “is_up”. It’s a string, of course. Notice the colon between the key “is_up” and the value, which is true, written without double quotes.
That is a boolean. This probably indicates that the interface status is up, because of the value true for the key “is_up”. Again, notice the comma after this key-value pair, because there is another key-value pair after it.
For these next two examples, the values are both strings, because they are contained within double quotes. Notice the standard JSON formatting again. The key is always a string, there is a colon between the key and value, and there is a comma after each key-value pair.
Now let’s look at the final pair. What data type is the value 1000? It’s a number, a numeric value not contained in double quotes.
And notice that this time there is no comma after the value. That’s because it’s the last key-value pair in this object. Okay, so that’s a basic JSON object with multiple key-value pairs inside.
Now, as you can see in the following example, objects are a valid data type for the value of a key-value pair. I’ve colored the curly brackets to make it a bit easier to see, but I want you to pause the video to try to interpret this data.
Find the key value pairs. It’s important that you get comfortable reading JSON-formatted data, as it’s clearly stated in the CCNA exam. Okay, so this example is one JSON object, indicated by the red curly brackets at the top and bottom.
Inside of this object there are two key-value pairs. Here’s one key-value pair. The key is “device”, a string of course, and the value is an object, indicated by the blue curly brackets.
So, we have an object inside of an object. Note that objects within objects are called ‘nested objects’. Now, within that value, the object, there are multiple key-value pairs.
“name”:”R1” is a key-value pair, “vendor”:”Cisco” is a key-value pair, and “model”:”1101” is a key-value pair. Now down here we have one more key-value pair, and again the value is an object, indicated by the purple curly brackets.
And inside of that object there are again more key-value pairs. I’m keeping this fairly easy to read by using indenting and using line breaks and different colors, but keep in mind that JSON data won’t always look like this.
The whitespace is insignificant and only used to make it easier for us to read, but applications themselves don’t need this whitespace. Okay, now there is one more JSON structured data type to look at, and that is the array.
An array is a series of values separated by commas. Note that it’s not a series of key-value pairs, that’s an object. It’s just a series of values.
And note that the values don’t have to be the same data type. One could be a string, and another a number, for example. So, here I’ve got one JSON object with two key-value pairs, and both of the values are arrays.
The first array, the value of the “interfaces” key, lists three interface names, with a comma between each of them. Note that there is no comma after gigabitethernet1/3, because it’s the last value.
And then the second array has two values, “Hi” and 5. Note that “Hi” is a string and 5 is a number, because it has no double quotes.
So, as you can see the values don’t necessarily have to be the same type, although often they will be. If you need to, pause the video now to look at this example and make sure you understand it, as well as the previous examples.
Just listening to me talk about JSON won’t help you interpret it if you don’t practice actually looking at and understanding JSON-formatted data.
For reference, here is the output of SHOW IP INTERFACE BRIEF, and a version of the same output in JSON. Note that this isn’t actual JSON output from a Cisco device, I just wrote this JSON data myself.
The point is that, although SHOW command outputs in the CLI are easy for us humans to read, they aren’t in a format that is easy for computers to understand.
JSON, on the other hand, is very easy for computers to understand, and if we format the text properly like this it’s also quite human-readable too.
Anyway, pause the video if you want to compare this SHOW command output and the JSON data. Okay, so we’ve looked at all of the six JSON data types.
There are four primitive data types: string, number, boolean, and null, and two structured data types: object and array. One more point, note that an object is sometimes called a dictionary.
Same thing, just a different name, but you should be aware of it. Okay, that’s all for JSON for now. We’ll take a quick look at XML and YAML, and then in the quiz for today’s video we’ll get lots of practice interpreting JSON data.
So, here’s XML. XML was developed as a markup language, but is now used as a general data serialization language. You know what a data serialization language is, but what’s a markup language?
Markup languages are used to format text, for example HTML, which you’ve probably heard of, is a markup language. XML is generally less human-readable than JSON, although it’s not so difficult to read.
Like JSON, whitespace is insignificant, although you’ll often see it displayed with spaces and line breaks to make it more human-readable. XML is also often used by REST APIs, same as JSON.
The general format you need to know is this: the key is tagged like this, with the value in the middle. For example, on Cisco IOS you can use a SHOW command, pipe the command with the FORMAT keyword, and it will display it in XML.
I used SHOW IP INTERFACE BRIEF, pipe, FORMAT, and here is the output in XML format. I’ve colored the output to make it easier to see, so I think you can easily see the keys and values.
We have two interfaces, each marked with ENTRY tags. Within each entry we have Interface, IP-address, OK, Method, Status, and Protocol keys, and the value is in the middle of those keys.
For example, here we have an <Interface> tag, with a value of GigabitEthernet0/0, and then a forward slash interface tag to close it off.
And here you can compare the regular SHOW IP INTERFACE BRIEF output with the XML-formatted output, without any coloring in the XML this time. XML is generally thought to be less human-readable than JSON, but it’s still not so hard to read when formatted like this.
So, that’s all I’ll say about XML. If you’re familiar with HTML, it looks quite similar. Remember basic characteristics like the key-value pair format for variables using tags, remember that whitespace is insignificant, and remember that it’s similar
to HTML and also used by REST APIs. If you want to take some time to examine this output pause the video, but now I’ll move on. Finally, let’s take a brief look at YAML.
The name YAML originally meant Yet Another Markup Language, but to distinguish its purpose as a data-serialization language rather than a markup language, it was repurposed to YAML Aint Markup Language.
That’s known as a recursive acronym by the way, when the acronym itself is part of the acronym. YAML is used by the network automation tool Ansible, which we’ll cover in another video.
It has all sorts of other uses too, of course, but its use in Ansible is relevant to our current topic, network automation. YAML is very human-readable, and whitespace is significant, unlike JSON and XML.
So, indentation is very important. We aren’t free to format the text however we want. YAML files start with three hyphens, and one hyphen is used to indicate a list.
Key value pairs are represented as key colon value. There are more rules to YAML, of course, but I’ll leave it there. Here’s an example of YAML.
This is the same SHOW IP INTERFACE BRIEF data I showed you in JSON and XML. It looks quite simple compared to JSON and XML.
And for comparison, here it is next to JSON. As you can see, both languages are quite human-readable, and they’re also used by applications to communicate data.
So, that’s all I’ll say about YAML. You don’t need to know it or XML as much as you need to know JSON, but you should know basic things such as the fact that whitespace is significant in YAML, and that it’s used in Ansible, because we’ll be studying Ansible in a future video.
Here’s what we covered in this video. I gave a basic introduction to data serialization and why we need it. Basically, it’s to provide a standard data format so applications can communicate the data with each other.
Then I introduced JSON, XML, and YAML, focusing primarily on JSON because it’s the one you really need to know for the CCNA. Now, in today’s quiz I will give more questions than usual so you can practice reading and interpreting JSON data.
And make sure to watch until the end of the quiz for a bonus question from Boson Software’s ExSim, the best practice exams for the CCNA.
Okay, let’s go to quiz question 1. In which of the following data serialization languages is whitespace significant?
Pause the video now to select the best answer. Okay, the answer is B, YAML. In JSON and XML whitespace is not significant, it has no meaning.
However in YAML whitespace is significant, so things like indentation are very important. Okay, let’s go to question 2.
Which of the following data serialization languages formats key-value pairs like this? Key, value, and then forward slash key. Pause the video now to select the best answer.
Okay, the answer is C, XML. XML uses HTML-like tags like this to indicate key-value pairs.
Okay, let’s go to question 3. Which of the following is NOT a valid JSON data type? Pause the video now to select the best answer.
Okay, the answer is E, key. Although JSON defines key-value pairs, key isn’t actually a data type.
All keys must be strings, and string is a valid data type. Okay, let’s go to question 4. Examine the JSON-formatted data.
Which of the following statements is true? Pause the video now to select the best answer. Okay, the answer is C, there are too many commas.
Here I’ve highlighted the extra comma. A comma is required between separate key-value pairs in an object, but you should not put a comma after the last key-value pair.
Okay, let’s go to question 5. Examine the JSON-formatted data. Which of the following statements is true? Pause the video now to select the best answer.
Okay, the answer is B, the value of “ip_interfaces” is an array. Now, within that array there are multiple objects, but as these square brackets indicate, the value itself is an array, that’s the JSON data type.
Okay, let’s go to question 6. Which of the following is an example of valid JSON-formatted data? Pause the video now to select the best answer.
Okay, the answer is D. Let me point out the errors in the others. In A, there is a comma after the “interfaces” key.
It should be a colon. In B, there is a colon after this value 5. A colon should only be used between the key and value of a key-value pair, it is not valid here.
And in C, there is a missing comma between gigabitethernet1/1 and gigabitethernet1/2. If multiple values are listed in an array, there must be a comma between each of them.
There are no problems with D, it is valid JSON data. Okay, let’s go to question 7. Examine the JSON-formatted data.
Which of the following statements is true? Pause the video now to select the best answer. Okay, the answer is A, the value of “is_up” is a boolean.
I’ve highlighted it here. It’s true, written without double quotes, so it is a boolean value. Booleans can be true or false, and they are written without quotes.
Okay, let’s go to question 8. Examine the JSON-formatted data. Which of the following statements is true? Pause the video now to select the best answer.
Okay, the answer is D, it is valid JSON data. No comma or curly brackets are missing, and whitespace is insignificant in JSON, so this is validly formatted.
We often use indentation and line breaks to make it easier to read, but whitespace like that is unnecessary and doesn’t have any meaning in JSON.
Okay, let’s go to question 9. Examine the JSON-formatted data. Which of the following statements is true? I think this question will be a bit more of a challenge.
Make sure each opening curly bracket has a closing curly bracket, each opening square bracket has a closing square bracket, each string has double quotes, etc.
Pause the video to select the best answer. Okay, the answer is C, a square bracket is missing. The value of the “routes” key is an array, it has an opening square bracket, but in the example on the left there is no closing square
bracket for that array. Again, make sure to check carefully that each opening bracket has a closing bracket. Let’s go to question 10.
Examine the JSON-formatted data. Which of the following statements is true? Pause the video now to select the best answer.
Okay, the answer is A, it is valid JSON data. There are no missing curly brackets or square brackets, and the value of “dhcp4” is a string, not a boolean.
Okay, I hope this practice was helpful for you. That’s all for the quiz. Now let’s take a look at a bonus question from Boson Software’s ExSim for CCNA.