Saturday, September 15, 2012

Python with JSon

JSon (JavaScript Object Notation) seems to be the new kid on the block when it comes to network information exchange over the network. JSon obviously is not new, it has been around for a long time, and is a pretty dominate API used for many sites, including Twitter. Glad to see various vendors starting to pick up this method.

In my personal opinion, JSon offers the same type of hierarchy structure as XML but it is more lightweight and therefore a better fit to bubble up messages from routers and switches that is more complex to express than syslog.

In a nutshell, a json object is a Python dictionary that have other nested objects:
Python 2.7 JSon Doc

Say we want to encode router1 into a json object, router1 has the following information:

Name: R1
ASN: 65001
Address:
  1 Infinite Loop
  Cupertino, CA
Interfaces:
  Eth1/1: To R2, 192.168.1.1
  Eth1/2: To R3, 192.168.1.3
BGP Neighbors:
  192.168.1.2, R2, 65002
  192.168.1.3, R3, 65003

We can simply create a dictionary:

>>> r1 = {'Name': 'r1', 'ASN': '65001', 'Address': {'Street': '1 Infinite Loop', 'City': 'Cupertino', 'State': 'CA'}, 'Interfaces': {'Eth1/1': {'Description': 'To R2', 'IP': '192.168.1.1'}, 'Eth1/2': {'Description': 'To R3', 'IP': '192.168.1.3'}}, 'BGP Neighbors': {'192.168.1.2':['R2', '65002'], '192.168.1.4': ['R3', '65003']}}
>>>

>>> r1['Name']
'r1'
>>> r1['BGP Neighbors']
{'192.168.1.4': ['R3', '65003'], '192.168.1.2': ['R2', '65002']}
>>>
>>>
>>>
>>> r1['Address']
{'City': 'Cupertino', 'State': 'CA', 'Street': '1 Infinite Loop'}
>>>
>>>
>>>
>>> r1['Interfaces']
{'Eth1/1': {'IP': '192.168.1.1', 'Description': 'To R2'}, 'Eth1/2': {'IP': '192.168.1.3', 'Description': 'To R3'}}
>>>

Then make it into a JSon object ready to be transferred over:

>>> import json
>>> R1_Json = json.dumps(r1)
>>>
>>>
>>> R1_Json
'{"BGP Neighbors": {"192.168.1.4": ["R3", "65003"], "192.168.1.2": ["R2", "65002"]}, "Interfaces": {"Eth1/1": {"IP": "192.168.1.1", "Description": "To R2"}, "Eth1/2": {"IP": "192.168.1.3", "Description": "To R3"}}, "Address": {"City": "Cupertino", "State": "CA", "Street": "1 Infinite Loop"}, "Name": "r1", "ASN": "65001"}'
>>>

On the other hand, if you received a JSon object, you can load it:

>>> import json
>>> New_R1 = json.loads(R1_Json)

note that the keys and values are unicode (indicated by the 'u' in front):

>>> New_R1
{u'ASN': u'65001', u'Interfaces': {u'Eth1/1': {u'IP': u'192.168.1.1', u'Description': u'To R2'}, u'Eth1/2': {u'IP': u'192.168.1.3', u'Description': u'To R3'}}, u'BGP Neighbors': {u'192.168.1.4': [u'R3', u'65003'], u'192.168.1.2': [u'R2', u'65002']}, u'Name': u'r1', u'Address': {u'City': u'Cupertino', u'State': u'CA', u'Street': u'1 Infinite Loop'}}
>>>

>>> New_R1['Name']
u'r1'
>>> New_R1['Address']
{u'City': u'Cupertino', u'State': u'CA', u'Street': u'1 Infinite Loop'}
>>>
>>> New_R1['Interfaces']
{u'Eth1/1': {u'IP': u'192.168.1.1', u'Description': u'To R2'}, u'Eth1/2': {u'IP': u'192.168.1.3', u'Description': u'To R3'}}
>>> New_R1['BGP Neighbors']
{u'192.168.1.4': [u'R3', u'65003'], u'192.168.1.2': [u'R2', u'65002']}
>>>

With this tool in hand, working with a NoSQL document-based database like MongoDB is pretty straight forward. Stay tuned for more on MongoDB (hint: PyMongo is really cool!).




No comments:

Post a Comment