Automation
Showing results for 
Search instead for 
Do you mean 

Expert Advice: Understanding JavaScript Object Notation (JSON)

by Juniper Employee on ‎03-20-2017 08:44 AM - edited on ‎03-21-2017 12:12 PM by Administrator Administrator (7,301 Views)

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

 

In this JSON encoding, you can query configuration using format=”json”. Junos OS also supports loading the configuration in this JSON encoding.

 

Examples of JSON encoding for various types of objects as per IETF drafts which Junos OS supports

 

1. Container, leaf, and leaf-list

"system" : {
  "host-name" : "my-vmx-1",
  "apply-groups" : ["g1", "g2"]
}

In the preceding example, system is a container, host-name is the leaf under system, and apply-groups is the leaf-list under system.

 

2. Junos OS metadata on the container

Junos OS supports some very powerful and useful features such as deactivating configuration
objects, protecting configuration objects, and so on.

Junos OS supports encoding such information in JSON as follows:

"system" : {
  "@" : {
    "inactive" : true,
    "protect" : true,
    "annotate" : "inactive and protected container of Junos config hierarchy"
  },
  "host-name" : "my-vmx-1",
  "apply-groups" : ["g1", "g2"]
}

In the preceding example, system is the container and contains additional Junos OS metadata. For example, it is marked as protected and is deactivated. It also includes comments.

 

3. Junos OS metadata on the leaf

"system" : {
  "host-name" : "my-vmx-1",
  "@host-name" : {
    "inactive" : true
  },
  "apply-groups" : ["g1", "g2"]
}

In the preceding example, the host-name leaf is deactivated.

 

4. Junos OS metadata on the leaf-list

"system" : {
  "host-name" : "my-vmx-1",
  "apply-groups" : ["g1", "g2"],
  "@apply-groups" : {
    "protect" : true
  }
}

In the preceding example, the entire apply-groups leaf-list is protected. Junos OS does not allow adding metadata on leaf-list elements.

 

5. List

"interfaces" : {
  "interface" : [
  {
    "name" : "xe-0/0/0",
    "description" : "first-ifd"
  },
  {
    "name" : "xe-1/0/1",
    "description" : "second-ifd"
  }  
  ]
}

In the preceidng example, interfaces and interface are lists containing two elements inside. It is encoded as a JSON array.

 

6. List with metadata

"interfaces" : {
  "interface" : [
  {
    "name" : "xe-0/0/0",
    "description" : "first-ifd",
    "unit" : [
    {
      "name" : "0",
      "description" : "first-ifl"
    },
    {
      "@" : {
        "inactive" : true
      }
      "name" : "1",
      "description" : "second-ifl"
    },
    {
      "name" : "2",
      "description" : "third-ifl"
    }      
    ]
  }  
  ]
}

In the preceding example, unit 1 is deactivated under the xe-0/0/0 interface.

 

7. Empty object

"interfaces" : {
  "interface" : [
  {
    "name" : "xe-0/0/0",
    "description" : "first-ifd",
    "unit" : [
    {
      "name" : "0",
      "description" : "first-ifl",
      "family" : {
        "inet" : [null]
      }
    }
    ]
  }  
  ]
}

In the preceding example, xe-0/0/0.0 has family inet configured, which contains nothing, so it is marked as empty.

** Load configuration in JSON – RPC example

<rpc>
  <load-configuration format="json">
    <configuration-json>
      .. .. ..
      .. .. ..
      .. .. ..
    </configuration-json>
  </load-configuration>
</rpc> 

** Get configuration in JSON – RPC example

<rpc>
  <get-configuration format="json"/>
</rpc>