6.7 Interpret JSON encoded data

Like XML, it’s easy to interpret JSON data.  If you look at a set of JSON data, you can probably guess what it means.

The JSON data is written in the format of Key:Value.  The key is the data before the colon, and the value is the data after the colon.  Each Key:Value pair is separated by a comma.  If a key has multiple values, they are enclosed in square brackets.

For example, the following data specifies the configuration of a switch port

{

     “speed”: “100”,

     “duplex”: “full”,

     “VLAN”: “20”

}

We can write the JSON data like this instead

{“speed”: “100”, “duplex”: “full”, “VLAN”: “20”}

The spaces don’t matter.  The curly brackets enclose the set of JSON data delivered from the server.  This is also known as an object.

We can write a script to interpret JSON data received from a server, but most programming languages include functions to automatically parse the data.

We can also create a JSON object.  An object is like a set of variables.  For example, a port configuration might be an object.  We enclose an object with {}.  For example, the configuration of Port 1 might be an object with the following data

{“speed”: “100”, “duplex”: “full”, “VLAN”: “20”}

An array is enclosed in [].  For example, the API might return the list of switch ports like

[“FastEthernet0/1”, “FastEthernet0/2”, “FastEthernet0/3”]

It could also return an object that includes an array, for example.

{

     “ListofACLs”: [“1”, “2”, “3”, “4”],

}

What if I have two variables – ListofACLs (List of ACLs), and ACLIPAddresses (ACL IP Addresses).  Each variable has an array with four variables.

When interpreting the JSON data, we need to understand that an object is surrounded by curly braces {}.  Inside the object is one or more variables.  The variable name is to the left of the colon and the variable value is to the right.

The value of a variable can be a single value, an array, or another object.  For example, we have returned an object showing the configuration of three ports, and the configuration of each port is an object with three variables

{

“FastEthernet0/1” : {“speed”: “100”, “duplex”: “full”, “VLAN”: “20”},
“FastEthernet0/2” : {“speed”: “1000”, “duplex”: “full”, “VLAN”: “20”},
“FastEthernet0/3” : {“speed”: “10”, “duplex”: “half”, “VLAN”: “20”}

}

I could also write it like this, so that it’s easier to read

{

“FastEthernet0/1” : {

“speed”: “100”,

“duplex”: “full”,

“VLAN”: “20”

},
“FastEthernet0/2” : {

“speed”: “1000”,

“duplex”: “full”,

“VLAN”: “20”

},
“FastEthernet0/3” : {

“speed”: “10”,

“duplex”: “half”,

“VLAN”: “20”

}

}

The entire JSON is an object (the switch is an object), and each variable is an object (each interface is an object).  We might have an API that returns the configuration of all the ports, and an API that returns the configuration of a single port.

I can write the JSON in any number of ways, adding spaces or tabs where necessary to make it easier to read, but when we send a JSON over the internet, the computers cut out all the white space.