Parse JSON data from Cisco Router with Python Script with example script

With the advent of Telemetry and the introduction of more programming capabilities to network devices we have the opportunity to fetch and analyse huge set of data which was not available so easily earlier. Most of the telemetry receiver applications provide you data in various format and one of the most widely available format is JSON. JSON has already set the stage for itself because of the ease of access and performance. It is very easy to read JSON data with Python.

In this article I will show a small script which I use to parse through JSON data and fetch an output of my choice.There can be many other ways, this is the one which I used recently.

Data Set :

As a data set I have a JSON file which was fetched from the Cisco XR router with the help of telemetry. Here you can download the complete file .

Below is a small section from the file, you can see that we have IP address of the router from where the data is coming and a lot of other information providing great set of information from the router. The question is how would you parse this file and fetch the the output like “input errors?” Lets discuss the parsing code in next section.

{
     "Source": "172.16.226.150:54595",
     "Telemetry": {
         "node_id_str": "ios",
         "subscription_id_str": "SUB",
         "encoding_path": "Cisco-IOS-XR-infra-statsd-oper:infra-statistics/interfaces/interface/latest/generic-counters",
         "collection_id": 19793,
         "collection_start_time": 1607459893784,
         "msg_timestamp": 1607459893784,
         "collection_end_time": 1607459893800
     },
     "Rows": [
         {
             "Timestamp": 1607459893797,
             "Keys": {
                 "interface-name": "Null0"
             },
             "Content": {
                 "applique": 0,
                 "availability-flag": 0,
                 "broadcast-packets-received": 0,
                 "broadcast-packets-sent": 0,
                 "bytes-received": 0,
                 "bytes-sent": 0,
                 "carrier-transitions": 0,
                 "crc-errors": 0,
                 "framing-errors-received": 0,
                 "giant-packets-received": 0,
                 "input-aborts": 0,
                 "input-drops": 0,
                 "input-errors": 0,
                 "input-ignored-packets": 0,
                 "input-overruns": 0,
                 "input-queue-drops": 0,
                 "last-data-time": 1607459864,
                 "last-discontinuity-time": 1607313550,
                 "multicast-packets-received": 0,
                 "multicast-packets-sent": 0,
                 "output-buffer-failures": 0,
                 "output-buffers-swapped-out": 0,
                 "output-drops": 0,
                 "output-errors": 0,
                 "output-queue-drops": 0,
                 "output-underruns": 0,
                 "packets-received": 0,
                 "packets-sent": 0,
                 "parity-packets-received": 0,
                 "resets": 0,
                 "runt-packets-received": 0,
                 "seconds-since-last-clear-counters": 0,
                 "seconds-since-packet-received": 4294967295,
                 "seconds-since-packet-sent": 4294967295,
                 "throttled-packets-received": 0,
                 "unknown-protocol-packets-received": 0
             }
         },

Python Code for Parsing

Now we know about the file details, let’s discuss about the code which can be used to fetch the data. Python provides JSON library which is useful to break the json data into the list and dictionary formats. Once the JSON data is broken into list or dictionary, it becomes easy to access the item in those data structures.

import json
from contextlib import redirect_stdout

def job():
        with open('Data.txt','r') as subdata:
            Data_load_to_Json = json.loads(subdata.read())
            Filtered_List = Data_load_to_Json['Rows']
            for i in range (0,len(Filtered_List)):
                NewDictionary=Filtered_List[i]['Keys']
                if NewDictionary['interface-name']=="GigabitEthernet0/0/0/0":
                    NewDictionary_2=Filtered_List[i]['Content']
                    with open('packets-received.txt', 'a+') as Output:
                        with redirect_stdout(Output):
                            print(NewDictionary_2['packets-received'])
                    with open('input-drops.txt', 'a+') as Output:
                        with redirect_stdout(Output):
                            print(NewDictionary_2['input-drops'])

job()

Here are the details of variables that I have used and it’s data types. The original data is loaded to json in dictionary format and from there I have fetched the data which comes in LIST format and from LIST, I further pulled the data with condition which comes in a Dictionary format which I again filtered to get my refined data(NewDictionary-2) on which I will work. The code provides you two files , packets-received.txt and input-drops.txt. You can fetch any details available about the interface in the given Data.txt file. In real world there will be many other interfaces and many other data which can be accessed easily with a slight change in the above provided code. In the above code you need to keep in mind that I need to reach to the value of the parameter which I need to see, for example if you need to see the value of input-errors you can adopt any method to reach to it. The easiest method is to have a smallest possible dictionary which contains this value along with key which is “input-errors”, and this is what I have tried to achieve with my code.

Output Files

Thanks for reading!

Leave a Reply