Step by Step ARM Templates - JSON 101 for IT Administrators
@20aman Aug 17, 2016Index of all blogs in this Step by Step ARM Templates series is located here: Step by Step Azure Resource Manager (ARM) Templates - Index
Azure Resource Manager (ARM) templates are written in JSON or JavaScript Object Notation. To understand ARM templates, you need to understand few quick basics about JSON. These will enable you to lay a great foundation which will enable you to understand ARM templates very easily.
JSON or JavaScript Object Notation (pronounce like "Jay-son") is a text-based data format that's designed to be human-readable, lightweight, and easy to transmit between a server and a web client. Its syntax is derived from JavaScript. Think of this as an even more compact version of XML files.
JSON is a popular notation for transmitting data through RESTful web services. The official internet media type for JSON is application/json
, and JSON files typically have a .json
extension.
To understand JSON we need to understand 3 main components. These components are like building blocks, using which you can build very complex JSON files.
1. Objects
Objects are the heart of JSON. Object denotes a real life object, e.g. an Employee. Just like a real life object, these have various properties and a value for each of these properties. E.g. An Employee will have Name property with value as John. Further, an employee object can have various another properties like Age, Salary, Department etc. So to denote an object in JSON you:
- One object will be represented by curly brackets. It will begin from opening curly bracket i.e.
{
and will end at closing curly bracket i.e.}
- Denote the property and corresponding values as
"key" : "value"
or"property" : "value"
pairs. - You can only use double quotes for Properties as they will always be of type string
- You will have double quotes around Values if they are of string type. You will not have any quotes in case of a number or a boolean value.
- Each property will be separated from next property by a comma
Note: Each JSON file is also a single JSON object. At root level it starts with an opening curly bracket i.e. {
and will end with closing curly bracket i.e. }
. There can't be any other objects at the root level. Think of this similar to how in an XML file there can be only one element at the root level.
Example Employee object is shown below:
{
"Name" : "John",
"Age" : 34,
"Department" : "Finance",
"Salary" : "100000",
"IsAdmin" : true
}
2. Arrays
Simply put, arrays are a collection of items. In JSON the square brackets represents an Array. E.g. An array of 3 employees will look like below:
[
{
"Name" : "John",
"Age" : 34
},
{
"Name" : "Mary",
"Age" : 32
},
{
"Name" : "Matthew",
"Age" : 29
}
]
3. Nesting of Objects
Now things get more interesting with nesting of Objects. What Nesting means is that one object can have it's property as another complex object. Don't worry if that sounds confusing. Let's understand that statement using an example. An Address where a person lives can be represented by an object. This object will look like below:
{
"StreetNumber" : "50",
"StreetName" : "Brian Harrison Way",
"Unit Number" : 22,
"City" : "Toronto",
"Country" : "Canada"
}
Now an Employee Object will have an Address object as one of it's property (because employee need to live somewhere). This new complex Employee object will look like below, with nested Address object as one of it's property:
{
"Name" : "John",
"Age" : 34,
"Department" : "Finance",
"Salary" : "100000",
"IsAdmin" : true,
"Address" : {
"StreetNumber" : "50",
"StreetName" : "Brian Harrison Way",
"Unit Number" : 22,
"City" : "Toronto",
"Country" : "Canada"
}
}
That's all there is to it. Now you can use these 3 components and build very complex json files/templates. Even the most complex template can be broken into these 3 components.
Below is a complex example with all 3 components.
{
"Department": "Finance",
"TotalEmployees": 2,
"Employees": [
{
"Name": "John",
"Age": 34,
"Department": "Finance",
"Salary": "100000",
"IsAdmin": true,
"Address": {
"StreetNumber": "50",
"StreetName": "Brian Harrison Way",
"Unit Number": 22,
"City": "Toronto",
"Country": "Canada"
}
},
{
"Name": "John",
"Age": 34,
"Department": "Finance",
"Salary": "100000",
"IsAdmin": true,
"Address": {
"StreetNumber": "50",
"StreetName": "Brian Harrison Way",
"Unit Number": 22,
"City": "Toronto",
"Country": "Canada"
}
}
]
}
The above JSON object denotes one department with name as Finance and total number of employees as 2. Then the "Employees" object is an array of 2 emplyees. Each emplyee object further have a complex property as Address, which is another object.
If you understood each of the 3 components, you should be able to build/understand most complex JSON files with ease.