How-To

How To Use JSON Files to Configure AWS Services

Here's a quick refresher on how JSON files work and how you can use them to configure AWS services.

Normally, I like to use my articles on this site to show you how to accomplish various tasks in an Amazon Web Services (AWS) environment. This time, however, I want to do something a little bit different. Being that many of the AWS services can be configured through the use of a JSON file, I wanted to take a step back and talk about what JSON files are and how they work. So, let's get started.

The name JSON is an acronym for JavaScript Object Notation. The file format is designed to be simple, human-readable, and some people believe that JSON files will eventually replace XML files because of their simpler structure. It isn't just the file structure that makes it appealing. Because JSON files are based on JavaScript, anyone who works in JavaScript will undoubtedly feel right at home when working with JSON files. There are, however, some minor syntax differences between JavaScript and JSON.

The first thing that you need to know about JSON files is that they're text files containing a series of key-value pairs (although some documentation refers to these pairs as name-value pairs). The basic concept of a key-value pair should be familiar to anyone who has ever created tags within AWS.

AWS allows you to apply tags to most, if not all, of the objects that you create in the AWS cloud. Tags are really nothing more than text-based key-value pairs that are used to classify an object, as shown in Figure 1. Suppose, for example, that you wanted to keep track of the applications that are running on all of your Amazon Elastic Compute Cloud (EC2) virtual machines (VMs). You might create a key named Applications, and use the key's value to store the name of the application that's running on a particular VM.

[Click on image for larger view.] Figure 1, AWS allows you to use tags to classify AWS objects.

JSON files use a structure that's very similar to that of tags. Where tags are used within AWS to classify objects, however, JSON files are commonly used as a means of performing automated configurations. A script might, for example, automate the creation of EC2 VM instances, and look to a JSON file to provide information such as the instance name, the guest OS, and the instance size.

It's also worth noting that JSON files are not unique to AWS environments. JSON files are an industry standard that's used in a variety of products and environments. So with that said, let's take a look at a very simple example of a JSON file.

The first program that's taught in pretty much any programming or scripting class is the somewhat cliché "Hello World" program.

So building on that concept, let's suppose that you've written a script that looks to a JSON file for a key that's named ScreenText, and then displays whatever value is associated with that key. For the sake of example, let's assume that the key's value is the phrase "Hello World." Here's what the JSON file would look like:

{
"ScreenText": "Hello World"
}

 

That's it. That's the whole thing. Both the key and the value are enclosed in quotation marks, and are separated by a colon. There's also a bracket symbol at both the beginning and the end of the file.

Of course, JSON files are commonly more complex than this. Typically a file will contain multiple key-value pairs. The pairs are separated by a comma. If, for example, the JSON file was designed to provide a first and last name, it might look something like this:

{
"First": "Brien",
"Last": "Posey"
}

 

As you can see, this looks exactly like what I showed you before, except that this JSON file contains multiple key-value pairs and the pairs are separated by a comma and a line break.

One of the really interesting things that you can do with a JSON file is to create a hierarchical structure. Suppose for a moment that I wanted to modify the previous JSON file to also include information about the car that the person drives. It might not be enough to simply add a key called Car, because there are a lot of different vehicle attributes that might be useful. For example, we might want to know the make, model, year, VIN, color and so on. What I can do in this case is to create a series of key-value pairs that correspond specifically to a vehicle. These pairs are included inside a separate pair of braces. Here's what the file might look like:

{
"First": "Brien",
"Last": "Posey",
"Car": {
  "Make": "Cadillac",
  "Model": "XTS",
  "Year": "2017"
  }
}

Notice that the Car key doesn't contain a value. Instead, I'm using the brace symbol, followed by a series of key-value pairs that correspond directly to the car. Incidentally, the data used in this file is just an example. I don't actually drive a Cadillac XTS. It was just the first car that came to mind.

About the Author

Brien Posey is a 22-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.

Featured

Subscribe on YouTube