Understanding JavaScript Object Notation (JSON)
Description
In my previous articles, I showed how you can use REST API to interact with Meraki dashboard and devices with both programs such as Postman and scripting using Python. However, for this article, I want to go into a bit of detail about JavaScript Object Notation, otherwise known as JSON, and how it is constructed. I feel this is important since I am seeing more traction in developers using it compared to XML (although XML is still widely used).
JSON Components
JSON makes it easy for information to be transferred between users and systems (and vise-versa) as it is simply text that is easy to read and, unfortunately/fortunately, limited in its customization. In a nutshell, JSON is a human-readable collection of data, objects, and arrays that can be accessed in a logical manner. For example, take a look at the following example:
{
"FirstName": "John",
"LastName": "Doe"
}
This is what JSON calls an object which is defined with {} brackets. An object is a collection of properties using name/value pairs separated by colons and separated by commas. A few things to note :
- When defining name/value pairs, you must use double quotes for names and strings. Valid JSON data does not use single quotes
- JSON does not use trailing commas. The only time you use commas is when you are defining more data after whatever you are defining. This means name/value pairs, objects, arrays, etc.
Now, lets take a look at another component of JSON: arrays. Arrays are defined with [] brackets and are simply an ordered list of values. Arrays can store multiple value types and can even contain multiple objects themselves. Take a look at the following example in which we build upon the last example:
{
"FirstName": "John",
"LastName": "Doe",
"Email": ["john.doe@test.local", "jdoe@test.local"]
}
While we have an object that contains two name/value pairs, we have also defined an array named Email with two entries. Notice the commas (which are very important)...we have added a comma after LastName since its not the last element in the object anymore. Before, I also mentioned that an array can actually be made up of objects as well. Take the email example...what if we wanted to define an alias along with each email address? That would be simple:
{
"FirstName": "John",
"LastName": "Doe",
"Email": [
{
"alias": "john.doe",
"address": "john.doe@test.local"
},
{
"alias": "jdoe",
"address": "jdoe@test.local"
}
]
}
Notice how we still define the array using the [] brackets, but this time, we added two objects as shown by the {} brackets. We still following the requirements of comma placement and defining name/value pairs. As you can see, JSON data can either be simple or complex but it is still very readable.
JSON Data Types
Now that we have seen basic examples of JSON object and arrays, lets take a quick look at the data types that can be used. Valid data types are as follows:
- Strings
- Numbers
- Objects (JSON objects)
- Arrays
- Booleans
- Nulls
However, note that data types cannot be a function, date, or undefined.
Closing Thoughts
While there is still a need to understand other formats such as XML and YANG, you may ask yourself why do I need to know this? Although this was a very quick and simple article, since I have been talking about API access in the last few articles, I wanted to talk about it a little.
This is the format that you will receive data in but also the format that you will use to send data to devices and is identified as MIME type application/json. You will use it in both HTTP headers Content-Type (client tells the server what kind of content it is sending) as well as Accept (clients notify the HTTP servers what content types it will accept). And, if we are talking about using Python to send or receive JSON data, we can easily parse the data by using simple for loops so that we can grab data out of the name/value pairs to help us automate every day tasks.
I also recommend using JSONLint to not only practice creating JSON data but also to validate data as it becomes more complex. As you practice more, you will see how easy it is to use JSON and will be able to do some great things with the data.
Happy scripting!
Very good article Sir, it was really easy to understand