Automation and Programmability · Topic 7 of 7

Interpreting JSON Encoded Data

Learn to read JSON: objects, arrays, key-value pairs and data types, how to find a value in nested data, and the syntax rules that cause errors.

  • 5 min
  • Exam objective 6.7
In a nutshell
  • JSON data is built from objects (curly braces) and arrays (square brackets).
  • An object is a set of key-value pairs. Keys are strings in double quotes.
  • The value types are string, number, boolean, null, object and array.

What it is

JSON is a text format for structured data, and this objective asks you to read a piece of it and say what it contains.

JSON (JavaScript Object Notation) is used by programs to exchange information, and most REST APIs return their answers in it. Despite the name, it is not tied to JavaScript. Nearly every language can read and write it.

It is popular because it is easy for people to read and easy for software to process. In network automation, you meet it when a controller returns a list of devices, when a device reports its interfaces, or when you send a configuration to an API.

How it works

JSON is built from objects and arrays that hold simple values and can be nested inside each other.

The building blocks

JSON has two structures and a few simple value types.

An object is a collection of key-value pairs inside curly braces. The key is always a text string in double quotes. A colon separates it from its value, and commas separate the pairs.

{
  "hostname": "R1",
  "model": "ISR4331",
  "uptime_days": 42,
  "managed": true
}

An array is an ordered list of values inside square brackets, separated by commas. The values can be of any type, including objects.

["GigabitEthernet0/0", "GigabitEthernet0/1", "Loopback0"]

The value types are:

TypeExampleNotes
String"R1"In double quotes
Number42 or 3.5No quotes
Booleantrue or falseLowercase, no quotes
NullnullMeans no value
Object{ ... }Curly braces, key-value pairs
Array[ ... ]Square brackets, ordered list

Notice the difference between "42" and 42. The first is a string, and the second is a number. Likewise "true" is a string, and true is a boolean.

Nesting

Objects and arrays can hold other objects and arrays. This is called nesting, and it is how JSON describes complex data.

Here is a device with a list of interfaces, each of which is an object:

{
  "device": {
    "hostname": "SW1",
    "interfaces": [
      {
        "name": "GigabitEthernet0/1",
        "enabled": true,
        "vlan": 10,
        "ip": null
      },
      {
        "name": "GigabitEthernet0/2",
        "enabled": false,
        "vlan": 20,
        "ip": "192.168.20.1"
      }
    ]
  }
}

Finding a value

To read nested data, follow the path from the outside in. Keys are followed by name, and arrays are followed by position. Positions in an array start at zero.

deviceobjectkey interfacesinterfacesarray of 2position 1second itemobjectkey vlan20number
  • The hostname is found at device, then hostname. The value is "SW1".
  • The interfaces are a list at device, then interfaces. It has two items.
  • The first interface is position 0. Its name is "GigabitEthernet0/1".
  • The second interface is position 1. Its vlan is 20.
  • The first interface has "ip": null, which means it has no address assigned.
  • The second interface is not enabled, because "enabled": false.

Written as a path in a program, the VLAN of the second interface is often shown as device.interfaces[1].vlan.

A useful reading habit is to ask three questions of each part: is it an object, an array or a simple value, what is the key, and what is the type of the value.

Syntax rules

JSON is strict, and small mistakes make the whole document invalid.

  • Keys and strings use double quotes. Single quotes are not allowed.
  • Pairs are separated by commas, but there must be no trailing comma after the last item.
  • Braces and brackets must be balanced. Every { needs a } and every [ needs a ].
  • JSON does not allow comments.
  • Keys inside one object should be unique.

A broken example, with two mistakes:

{
  'hostname': "R1",
  "uptime_days": 42,
}

The first key uses single quotes, and there is a trailing comma after the last pair.

JSON compared with XML and YAML

All three describe structured data, but they look different.

FormatLooks likeNotes
JSONBraces and bracketsCompact, the usual REST format
XMLOpening and closing tagsMore verbose
YAMLIndentationEasy for people to write, used by Ansible playbooks

Many tools can convert from one to the other.

What to remember for the exam

Eight facts cover this objective.

  • JSON data is built from objects (curly braces) and arrays (square brackets).
  • An object is a set of key-value pairs. Keys are strings in double quotes.
  • The value types are string, number, boolean, null, object and array.
  • Strings use double quotes. Numbers and booleans do not.
  • Array positions start at zero.
  • Read nested data by following the path of keys and positions.
  • No trailing commas, no comments and no single quotes are allowed.
  • JSON is the usual data format for REST APIs.
Exam tip

To read a value, walk from the outside in. Use the key name for an object and a number starting at zero for an array.

Common mistakes

These six slips are the usual ones.

Counting arrays from one. The first item is at position zero.

Mixing up object and array. Curly braces hold named values. Square brackets hold an ordered list.

Treating "42" as a number. If it is in quotes, it is a string.

Adding a trailing comma. Many editors accept it, but JSON does not.

Confusing null with false or zero. Null means there is no value at all.

Losing track of nesting. Go through the structure layer by layer. Each opening brace or bracket has a matching closing one.

Watch out

The second item of an array is position 1, not 2. Counting from one is a common reading error.

Check yourself

Work through these three prompts.

  • What is the difference between a JSON object and an array, and what is a small example of each?

    An object is a set of key-value pairs in curly braces, such as {"hostname": "R1"}, while an array is an ordered list of values in square brackets, such as ["GigabitEthernet0/0", "Loopback0"].

  • In the nested device example, what is the path to the second interface's name?

    device.interfaces[1].name, because array positions start at zero, so the second interface is position 1.

  • What are two syntax mistakes that make a JSON document invalid?

    Using single quotes instead of double quotes, and leaving a trailing comma after the last item.

Topic 7 of 7 in Automation and Programmability

Test yourself on Automation and Programmability

The full Cisco CCNA 200-301 question bank has 1,206 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Cisco. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.