A JSON primer for administrators
Introducing JSON data-interchange format
If you have worked with different programming or scripting languages, each of them have their own data representation. In this era of cloud and mobile services, the services built using one programming language are generally consumed by a myriad of clients built in various languages. Take an example of services offered by Microsoft Azure. Most or all of them are written in C# and offer APIs with JSON as the output data format. We can consume these services in PowerShell, Python, and many more languages with libraries to translate the JSON data into a native data representation within those languages. This is how we have been using XML for a long time. So, this should be straightforward to understand. Then, why JSON? This topic is highly debatable. However, I like to quote just one example. You can read rest here.
Now, let us see the same representation in JSON. Don’t worry about the details of the JSON representation but just see how clean it is compared to XML.
What we have seen above is a very simple example. Imagine the verbosity of XML when the data that needs to be represented is huge. This is certainly one of the reasons for me to choose between XML and JSON. With this background, let us go into the details of JSON data format.
To start with, the general syntax for writing JSON objects is as follows:
You can nest objects in JSON. Here is a generic example.
Whether we need to enclose the value within double quotes or not depends on the type of the data.
Data Types in JSON
A JSON value can be of a basic data type such as string, number, and boolean. You can create ordered list of values such as arrays. And, as shown above, you can use unordered key-value pairs within JSON. JSON also supports empty values denoted by null.
Here are some examples of data types and how they are used in JSON.
In JSON data format, numbers represent a sequence of digits. There is no differentiation between floats, doubles, and so on. JSON values cannot be hexadecimal or octal. Here is an example of representing numbers in JSON.
As you see in the above example, we can represent different types of numbers including exponential and negative numbers. When the value you want to store in JSON format is a number, do not enclose it within the quotes.
Like every other programming or scripting language, a string represents a sequence of characters.
I have included several ways of representing string values in the above example. You see that all the values are enclosed within double quotes. Using single quotes is not allowed in JSON. When you need to use double quotes within the value string, you need to escape the same using the escape sequences. There are other escape sequences also shown in the above example. If you need to include special Unicode characters derived from hex representation, the same can be done using \u escape sequence.
Boolean values can be represented in JSON using true or false.
Remember that the values representing Boolean values should be always in lower case. Using True and False is not valid.
You can also create empty values by assigning null to a key.
Make a note that the value you assign must be null in lowercase. Using Null (uppercase) results in an error.
Arrays are an ordered list of values. Within the JSON data format, these values are enclosed within square brackets ([ and ]).
As you see in the example, you can assign the ordered lists or arrays to JSON keys. It is possible to mix data types (see Array3) within the arrays. However, this is not recommended as not all programming languages support mixed data types in arrays. So, when the JSON data is passed on to another language, there may be errors. Avoid mixing data types within in arrays in JSON.
Finally, objects within JSON data are the unordered set of key-value pairs. Essentially, by using objects within JSON data, you create nested JSON data representation.
The above example is a comprehensive one. It shows all data types along with nested objects. This ends our discussion on the data types in JSON. Now, how do you validate whether your JSON format is valid or not? I simply use the ConvertFrom-Json cmdlet.
This is just a primer like I mentioned earlier. I suggest that you start looking at using JSON wherever possible. It will be an essential skill to have. I will show you why in my future posts here! 🙂Share on: