Components of JSON-encoded data
Baha Abu-Shaqra, PhD (DTI uOttawa)
Network Engineering (career change)
JSON (JavaScript Object Notation) is a widely-used data serialization language. REST APIs often use JSON, XML, and YAML. This post looks at the components and structure of JSON-encoded data. This post explores the six JSON data types (string, number, boolean, null, object, and array) and how JSON variables are formatted.
This post covers topic 6.7 Recognize components of JSON-encoded data (Section 6.0 Automation and Programmability) of the CCNA exam topics list.
You may also be interested in Automation and programmability.
Before we dive in, I try to make the world a little better. You're invited to read my letter to uOttawa President?Jacques Frémont about how to easily implement policy reforms to prevent supervisor bullying of uOttawa students: uOttawa President Jacques Frémont ignores university bullying problem. You may also be interested in How to end supervisor bullying at uOttawa.
Data serialization and variables
Data serialization provides a standard data format so applications can communicate with each other. Data serialization is the transformation of data into a consistent (standard) format that enables its storage (e.g., in a file) and transmission between software applications.
Data serialization languages such as JSON, XML, and YAML allow us to represent variables with text. Variables are containers that store values.
Here are some examples of JSON variables (within a JSON object):
{“interface_name”: “G1/1”,
“status”: “up”,
“ip_address”: “192.168.1.1”,
“netmask”: “255.255.255.0”}
In the above example, “interface_name” is a container. And “G1/1” is a value. “status” is a container, and “up” is a value.
A basic JSON variable is a simple key-value pair. The key is a string that identifies the value. A string is a text value surrounded by double quotes (a string is one of four JSON primitive data types).
There are two categories of JSON data types: 1) primitive data types (string, number, boolean, and null), and 2) structured data types (object and array).
JSON keys are always strings. The value can be any valid JSON data type (string, number, boolean, null, object, array).
Here is an example of a basic JSON variable:
"name": "John Doe"
This variable has the key "name" and the value "John Doe". The value is a string, which is indicated by the double quotes around it.
Here is another example of a basic JSON variable:
"age": 30
This variable has the key "age" and the value 30. The value is a number (numbers are not enclosed in quotes).
Here is another example of a basic JSON variable:
"hobbies": ["hiking", "camping", "reading"]
This variable has the key "hobbies" and the value ["hiking", "camping", "reading"]. The value is an array of strings, which is indicated by the square brackets around it.
Here is an example of a JSON object with several key-value pairs inside.
This JSON object represents a person named John Doe. It includes his name, age, occupation, address, and hobbies. The address and hobbies are themselves JSON objects and arrays, respectively.
Variables are also used in XML and YAML data formats to store data of various types.
In XML, variables are represented by XML elements, which can contain text, attributes, and other child elements. For instance, the following XML snippet defines a variable named "name" and stores the value "John":
<person>
<name>John</name>
</person>
In YAML, variables are represented by key-value pairs, where the key identifies the variable name and the value represents the variable's content. For example, the following YAML snippet defines a variable named "age" and stores the value 30:
age: 30
JSON data types
JSON is an open standard file format and data interchange format that uses human readable text to store and transmit data objects.
JSON was derived from JavaScript, but it is language-independent and many modern programming languages are able to generate and read JSON data.
In JSON, whitespace is insignificant. Spaces and line breaks do not have meaning. JSON is standardized in RFC 8259.
JSON data types can be classified into two groups.
1. JSON primitive data types (4 types)
1.1. String: a text value surrounded by double quotes.
1.2. Number: a numeric value.
1.3. Boolean: a data type that has only two possible values, true and false.
For example, on an OSPF enabled interface, a variable "passive" would have the boolean value "true" if the interface is passive. If the interface is not passive, the variable would have the boolean value "false".
1.4. Null: a value representing an intentional absence of any object value.
2. JSON structured data types (2 types)
JSON represents data in two ways:
2.1. Object (dictionary): an unordered (i.e., the order does not matter) list of key-value pairs (variables).
The following example shows objects as a valid data type for the value of a key-value pair. Let’s find the key-value pairs.
This example is one JSON object, indicated by the red curly brackets at the top and bottom.
Inside of this object there are two key-value pairs:
First, “device” is the key and the value is an object, indicated by the blue curly brackets. So we have an object inside of an object. Note, objects within objects are called nested objects. Within that value, the object, there are multiple key-value pairs. “name”: ”R1” is a key-value pair, “vendor”: ”Cisco” is a key-value pair, and “model”: ”1101” is a key-value pair.
Second, “interface_config” is the key and the value is an object, indicated by the purple curly brackets. Inside of that object there are more key-value pairs.
2.2. Array: a series of values separated by commas.
An array is an ordered collection of values, and each value in an array can be of any JSON data type, including numbers, strings, booleans, objects, or even other arrays. Arrays are zero-indexed, meaning that the first element in an array has index 0, the second element has index 1, and so on.
Square brackets ([ ]) are used to demarcate arrays in JSON variables.
Here is an example of a JSON object with an array.
领英推荐
In this example, we have three key-value pairs. The hobbies key has an array as its value. The hobbies property is an array of strings. The first element in the array is "coding", the second element is "reading", and the third element is "traveling".
For reference, here is the output of show ip interface brief, and a version of the same output in JSON.
XML (Extensible Markup Language)
XML is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. XML is widely used for data storage and transmission, and it is the basis for many popular web technologies, such as RSS and SOAP.
XML was developed as a markup language, but is now used as a general data serialization language.
For reference, HTML is also a markup language, like XML based on SGML (standard generalized markup language).
In HTML, you use tags to create HTML elements. Many elements have an opening tag and a closing tag. For example, all web pages start with the html element, also called the root element:
<html>content (text or additional html elements called children)</html>
To create the html element, you write an opening <html> tag followed by a closing </html> tag. Everything else in your web page (all other page elements) goes between these two opening and closing html tags.
<html>
<p>child</p>
<p>child</p>
</html>
The <p> tag is the HTML element for a paragraph. The text "child" is the content of the paragraph element. The paragraph element also has an attribute called class, which has a value of "paragraph". This attribute tells the browser how to style the paragraph.
<p class="paragraph">child</p>
The opening tag can have one or more attributes which are key-value pairs that can customize the appearance or behavior of the element. The key is "class" and the value is "paragraph". The class attribute is used to identify the element as a paragraph and to apply any CSS rules that are defined for the class "paragraph".
Similarly, XML documents are made up of tags and attributes.
The general format of XML data: the XML key is tagged like this, with the value in the middle.
<key>value</key>
On Cisco IOS you can use a show command, pipe the command with the format keyword, and it will display it in XML.
For example, here we have two interfaces, each marked with entry tags. Within each entry we have Interface, IP-Address, OK, Method, Status, and Protocol keys. The value is in the middle of those keys.
Notice, for example, in the first interface entry, we have a tag <Interface>, with a value of GigabitEthernet0/0, and then a forward slash interface tag to close it off </Interface>.
XML is generally less human-readable than JSON. Like JSON, whitespace is insignificant in XML, although you will often see XML data displayed with spaces and line breaks to make it more human-readable.
For reference, here you can compare the regular show ip interface brief output with the XML-formatted output.
YAML (YAML Ain't Markup Language)
YAML is a human-readable data serialization language commonly used for configuration files and in applications where data is being stored or transmitted.
YAML targets many of the same communications applications as XML but has a minimal syntax which intentionally differs from SGML, the ancestor of HTML and XML.
YAML uses both Python-style indentation to indicate nesting, and a more compact format that uses [...] for lists and {...} for maps. However, YAML forbids tab characters for indentation, so only some JSON files are valid YAML (version 1.2).
YAML is used by the network automation tool Ansible. Unlike JSON and XML, in YAML whitespace is significant. So indentation is very important. We cannot format the text however we want.
Some basic YAML rules:
Here’s an example of YAML, the same show ip interface brief data example used in JSON and XML. For comparison, here it is next to JSON.
YAML looks quite simple compared to JSON and XML.
Other posts in this automation and programability series
Key references
Bard, Google AI. "JSON variable examples" (and several follow-up questions). Bard, Google AI, 2023-11-08. Accessed 2023-11-08.