Watch on Instagram

Network Bytes · Episode 66 · CCNA 6.7

"Reading JSON in 60 seconds"

JSON is the text format most network APIs reply in. Curly braces hold an object of labels and values, square brackets hold an ordered list, and values are text, numbers, true, false, null, or more objects and lists.

Posted · About a 1 minute watch, 3 minute read · Take the quiz

A labeled form, written as text

When a script asks a network device for information through an API, the answer usually comes back as JSON (JavaScript Object Notation). It is plain text, so you can read it yourself, but it is also strict enough that a program can pick it apart without guessing. Think of a paper form: every box has a printed label and something written in it. JSON is that form, written out in one line of text.

Objects: curly braces, labels and values

An object starts with { and ends with }. Inside are pairs: a label in double quotes, a colon, then the value. Pairs are separated by commas:

{ "name": "Gi0/1", "speed": 1000 }

Here "name" is a label and "Gi0/1" (GigabitEthernet0/1) is its value. The JSON standard calls the label a name; most people and tools call it a key. The order of the pairs inside an object does not matter, so a program looks a value up by its label, not by its position.

Values: text, numbers, true/false, null

A value can be:

  • text (a string) in double quotes, like "Gi0/1"
  • a number, like 1000, with no quotes
  • true or false, with no quotes
  • null, which means "nothing here"
  • another object { } or a list [ ]

The quotes matter: "1000" is text, while 1000 is a number, and "true" is just the word true, not the value true.

Lists: square brackets, in order

A list (the standard calls it an array) starts with [ and ends with ], with commas between the items. Unlike an object, a list keeps its order: the first item stays first. Because a list can hold objects, an API can send back a list of interfaces, each one its own little form:

[ {"name": "Gi0/1"}, {"name": "Gi0/2"}, {"name": "Gi0/3"} ]

To read a reply like that, find the brackets first (a list), then look inside each pair of braces (one object per item), then read each label and its value.

Try it yourself

Type this into any text editor and check each piece: {"hostname": "R1", "vlans": [10, 20, 30], "ssh": true, "banner": null}. How many labels does the object have? Which value is a list, which is true/false, and which one means nothing? Then paste it into your browser's developer console (F12, Console tab) as JSON.parse('...') and see the same structure laid out for you.

Good to know

  • JSON is defined in RFC 8259 and ECMA-404.
  • An object is an unordered set of name/value pairs in curly braces; names are always double-quoted strings.
  • An array is an ordered list of values in square brackets, separated by commas.
  • Values are strings, numbers, true, false, null, objects or arrays.
  • Strings need double quotes; numbers, true, false and null are not quoted.
  • The video says "labels" for names (keys) and "list" for array.
  • "Gi0/1" is short for GigabitEthernet0/1; the interface data in the video is an example, not output from a specific device.

For CCNA learners

This lines up with CCNA 200-301 exam topic 6.7, interpret JSON encoded data: be able to look at a JSON reply, tell objects from arrays, match each key to its value, and spot the value types.

Answer to the comment question

2. The list holds three objects. Gi0/1 has "up": true, Gi0/2 has "up": false, and Gi0/3 has "up": true, so two interfaces, Gi0/1 and Gi0/3, have up set to true. Reading it is the same every time: the square brackets say "list", each pair of curly braces is one interface, and inside each one you look up the "up" label and read its value.

Test your knowledge

Quick quiz: "Reading JSON in 60 seconds"

4 multiple-choice questions. Pick one answer for each, then check how you did.

1What do curly braces { } hold in JSON?
2In {"name": "Gi0/1"}, what is "name"?
3Which of these is a valid JSON value that means "nothing"?
4What do square brackets [ ] hold?

Sources

Video transcript

How do you read the text an API sends back?

Most of the time, it arrives as JSON. Think of it as a labeled form.

Curly braces hold an object: a set of labels and values. Each label sits in quotes, then a colon, then its value. Like: name, colon, Gi0/1.

Values can be text in quotes, numbers, true or false, or null, which means nothing.

Square brackets hold a list, in order, with commas between the items. And a list can hold objects, so you get a list of interfaces, each one its own little form.

This JSON lists three interfaces. How many have up set to true? Comment your answer below!

That's your byte.

More from Network Bytes

All Network Bytes lessons