JavaScript Object Notation

In this lesson we’re going to be talking about JSON, which is JavaScript Object Notation. we had touched on this very briefly in our last lesson when we did an overview of what data serialization techniques are and a few of them that exist. In this video we’re going to be taking a bit of a deeper dive into JavaScript Object Notation and the particular features and formatting of it.

First up, some basics of JSON.

Basic properties of JSON

JSON is case sensitive. A capitalization in the beginning of your JavaScript Object name or of your list’s name does matter. It is case sensitive. JSON does however ignore white space or indentation.

Indentation can be done for easier human reading, so it is acceptable to have whitespace in a JSON file, however when the JSON interpreter goes to read and interpret the information, all of that whitespace and indentation is just ignored.

Something that is a little different about JSON from other data serialization techniques is that there is no way to add comments to JSON code. Comments in code are sections which are not interpreted by the computer and are only for the human reader. Adding a comment or describing something, so that you could describe what a variable does, or a particular section of your configuration, or perhaps even giving a warning to a future user of your code saying particular values throw errors, all of these cases for comments can be very useful. However, JSON simply does not have a method of adding comments to its files.

For the actual formatting and data structure of JSON, it uses braces {} for objects and brackets [] for arrays. JSON objects are the same data structure of dictionaries as we discussed in the previous lesson, and arrays are the same data structure as lists. As we discussed previously, there’s different terms used for these same data types where an array is a countable ordered list of data, whereas an object or a dictionary is an unordered but highly indexed key-value mapping. So, let’s talk about the structure of each of our basic data types as well as our complex data types, and how they are notated within JSON


A sample string with special characters like quotations, carriage returns and backslashes escaped

First up, strings. For JSON, the escape character, backslash ( \ ), is used for special characters. As an example here, we need to escape the quotation marks around The Big One as well as put a special character for new line or a carriage return which is \n here in this case when we escape these special characters this would be our result in JSON notation.

A JSON string is anything in double quotes, with a list of escapable special characters

This diagram lists out the different special characters or escaped characters that can be done in JSON. We can have a solidus or reverse solidus, ie. slash or backslash, and we need to escape quotation marks. We can also do \b for a backspace, \f for a form feed, \n for a line feed, or a \r for a carriage return. We can also print a tab character with \t, and then \u will allow us to input any four hex digits to get any hexadecimal set character identifier.


A JSON number can contain any of the above formats

Moving on to numbers and how numbers are formatted in JSON. Numbers are represented in standard decimal format or in scientific notation. Scientific notation is represented with a lowercase or capitalized ‘e’ character representing 10 to the power of some number. Where 8.6e5 is 8.6 * 10^5 = 8.6* 100,000. Our diagram here is showing the structure of a number representation in JSON. In the beginning we can either have a minus sign or not to indicate a negative number, then we can either have a zero to indicate that we have some fraction where we would have a decimal point after the zero, or we can have some set of numbers being digits 1 through 9.

example numbers in JSON and binary

So in our first example above, our first number is a digit and then we just have standard decimal notation for digits here of 1023.

In our second example, our first number is a zero, then we have a decimal point with a decimal after. Third example is a negative number where we have our minus sign preceding our first digit and then it is a fraction after that.

Now, scientific notation you may or may not be used to that, here we can show our exponent which is the last item after our decimal number. Our exponent is represented with lowercase or capital “e” and then either a positive or negative exponent. As mentioned above, these are to represent 10 to the power of something. So 1.1*e5 or 1.1*e-5 would either be 110,000 or 0.000011.

As we see here for our scientific notation, if you’re unfamiliar with scientific notation you won’t really have to know that much for the exam, so I wouldn’t necessarily worry about it if you’re not real quick with scientific notation.

JSON Objects

A JSON object can contain any of the above formats

great so moving right along past our basic data types of strings and numbers, to a more complex data type, the JSON object.

A JSON object is a set of key-value pairs, as we described in the previous video. JSON objects are notated with braces ( { } ) and these can have nested objects or arrays.

Example JSON object with nested objects and arrays

In the example above, we see the open and close braces at the very top and bottom, indicating this is a JSON object. Next we have our first item which is the key host-name, and the value is the string Router 1. Then we have a comma to indicate that is the end of our first key value pair and that we have another key value pair.

If you only have a single key value pair in your object, no comma as necessary at the end. As well, the last key value pair in your object does not have a comma at the end.

So, this interfaces key actually has a very interesting and complex data type for a value. We see the square bracket and then the brace after. So, we’ll get to the square bracket in the next heading, that the square bracket indicates an array in JSON terms, or a list if you’re used to it in Python terms. Then we have the brace, as we see here is another nested object. So we actually have a JSON array which has two items in it, both are JSON objects, which have several key-value pairs.

This has a name key-value pair, and an address key-value pair in each of these objects.

JSON Arrays

A JSON array can contain any of the above formats

Moving on to JSON arrays. Arrays are ordered sets of values. These do not have key-value pairs, however it is just an ordered set of values. The notation for an array are square brackets as we see in the example up here. The opening square bracket and the closing square bracket is an array.

Arrays can contain a variety of data types

Our example above with three elements, it has a string element, a number element and then also an object element. We can have nested objects and arrays within our array. So we could have some array open, we could have 22 as an element, and we could have some other array as an element, or an object or string.

For a full lab demonstration of reading and printing JSON data with Python, please watch the full video lesson below!

Only subscribers can view the full content. Join Now!

Scroll to top

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

CiscoLessons will use the information you provide on this form to send occasional (less than 1/wk) updates and marketing.