Introduction

JSON, JavaScript Object Notation, is one of the usual suspects when it comes to network automation. YAML and XML being the other two. It’s easy for machines to parse and generate and the readability is good, better than XML, although YAML is easier for humans to read.

JSON is based on a subset of the JavaScript programming language, as the name implies.

JSON, just like YAML, supports single values, lists, and key/value pairs.

JSON is commonly used to interchange between different formats.

Syntax

JSON has no requirement for indentation or white space, which YAML has. That said, to make it human readable, it still makes sense to use white space and spaces, most likely either two or four.

  • Strings MUST use double quotes
  • Object literal names MUST be lowercase (null, false, true etc)
  • Special characters need to be escaped
  • { says “begin object”
  • } says “end object”
  • [ says “begin array”
  • ] says “end array”
  • : separates key and value in key/value pair
  • , separates key/value pair in an object or separates values in an array, think of it as “expect another one”

Data Types

JSON supports the following data types:

  • Object
  • String
  • Number
  • Boolean
  • Null
  • Array

We’ll go through them all in order starting with an object. An object is a list of key/value pairs surrounded in curly braces. Look at the example below:

{
  "vendor": "Cisco",
  "HQ": "San Jose",
  "employees": 76000
}

It’s also possible to have nested objects:

{
  "vendor": {
    "name": "Cisco",
    "device": {
      "router": {
        "name": "ASR 1001-X",
        "ipsec": true,
        "numports": 2
      }
    } 
  }
}

We already used a string in the example above but a string is essentially just anything within double quotes:

{
  "name": "Cisco"
}

We also used number previously but here’s an example showing that JSON supports integers and floating values:

{
  "integer": 7,
  "float": 3.14
}

When it comes to boolean, there is only true and false and they need to be all lowercase:

{
  "localBreakoutEnabled": true,
  "ipsEnabled ": false
}

There is also the null data type. Let’s say you have a key that is bgpPeers to indicate the number of BGP peers. What should you use for a device that doesn’t run BGP? Zero? That might indicate the number of BGP peers is relevant, which it wouldn’t be if you are not running BGP to start with. Null would be a more suitable use to describe that the number is not relevant.

{
  "bgpPeers": null
}

Finally we have the array. It’s a list that contains strings, numbers, booleans etc.

{
  "isrs": [
    "ISR1100",
    "ISR4200",
    "ISR4300",
    "ISR4400"
  ]
}

To show you what a more complete JSON file might look like, here’s an example I found from Cisco showing applying contracts using JSON:

{
  "src_name": "App",
  "dst_name": "Web",
  "whitelist": [
    {
      "port": [0, 0],
      "proto": 1,
      "action": "ALLOW"
    },
    {
      "port": [80, 80],
      "proto": 6,
      "action": "ALLOW"
    },
    {
      "port": [443, 443],
      "proto": 6,
      "action": "ALLOW"
    }
  ]
}

Now, there’s a lot going on here so let’s go through it line by line.

Line 1 is to specify that this is an object.

Line 2 maps the string App to the key src_name. A comma indicates more key/value pairs or values are coming.

Line 3 maps the string Web to the key dst_name. A comma indicates more key/value pairs or values are coming.

Line 4 creates an array named whitelist.

Line 5 is the start of a new object.

Line 6 maps an array consisting of [0, 0] to the key port. A comma indicates more key/value pairs or values are coming.

Line 7 maps the number 1 to the key proto. A comma indicates more key/value pairs or values are coming.

Line 8 maps the string ALLOW to the key action. There is no comma because no further key/value pairs or values are part of this object.

Line 9 is the end of this object.

There are then two other objects from line 10 to 19.

Line 20 ends the array whitelist.

Line 21 ends the object.

I hope this short intro helps you with your DevAsc studies. If you want another example of JSON, here’s one from my friend Nick Russo.

It’s also useful to validate your JSON in a tool. Good luck with your studies!

DevAsc – Introduction to JSON
Tagged on:             

4 thoughts on “DevAsc – Introduction to JSON

Leave a Reply to Kenyone Cancel reply

Your email address will not be published. Required fields are marked *