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
  1 Infinite Loop
  Cupertino, CA
  Eth1/1: To R2,
  Eth1/2: To R3,
BGP Neighbors:, R2, 65002, 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': ''}, 'Eth1/2': {'Description': 'To R3', 'IP': ''}}, 'BGP Neighbors': {'':['R2', '65002'], '': ['R3', '65003']}}

>>> r1['Name']
>>> r1['BGP Neighbors']
{'': ['R3', '65003'], '': ['R2', '65002']}
>>> r1['Address']
{'City': 'Cupertino', 'State': 'CA', 'Street': '1 Infinite Loop'}
>>> r1['Interfaces']
{'Eth1/1': {'IP': '', 'Description': 'To R2'}, 'Eth1/2': {'IP': '', '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": {"": ["R3", "65003"], "": ["R2", "65002"]}, "Interfaces": {"Eth1/1": {"IP": "", "Description": "To R2"}, "Eth1/2": {"IP": "", "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'', u'Description': u'To R2'}, u'Eth1/2': {u'IP': u'', u'Description': u'To R3'}}, u'BGP Neighbors': {u'': [u'R3', u'65003'], u'': [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']
>>> 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'', u'Description': u'To R2'}, u'Eth1/2': {u'IP': u'', u'Description': u'To R3'}}
>>> New_R1['BGP Neighbors']
{u'': [u'R3', u'65003'], u'': [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!).


  1. Thanks for providing good information,Thanks for your sharing python Online Course

  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or learn thru Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai

  3. Thanks you for sharing the article. The data that you provided in the blog is infromative and effectve. Through you blog I gained so much knowledge. Also check my collection at machine learning online training Blog

  4. I like your blog, I read this blog please update more content on python, further check it once at python online course

  5. Some truly interesting information, well written and broadly user pleasant. Online JSON Formatter

  6. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
    Advanced AWS Training in Bangalore | Best Amazon Web Services Training Institute in Bangalore
    Advanced AWS Training Institute in Pune | Best Amazon Web Services Training Institute in Pune
    Advanced AWS Online Training Institute in india | Best Online AWS Certification Course in india
    AWS training in bangalore | Best aws training in bangalore

  7. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's. 
    microsoft azure training in bangalore
    rpa training in bangalore
    best rpa training in bangalore
    rpa online training

  8. Resources like the one you mentioned here will be very useful to me ! I will post a link to this page on my blog. I am sure my visitors will find that very useful
    Best Devops online Training
    Online DevOps Certification Course - Gangboard

  9. I have picked cheery a lot of useful clothes outdated of this amazing blog. I’d love to return greater than and over again. Thanks! 
    I have picked cheery a lot of useful clothes outdated of this amazing blog. I’d love to return greater than and over again. Thanks! 

  10. Thanks For Sharing The Information The Information Shared Is Very Valuable Please Keep Updating Us Time Just Went On Reading The article Python Online Course Hadoop Online Course Aws Online Course Data Science Online Course

  11. Generous leads are cardinal for any business, this incorporates little Digital Marketing Company, medium and enormous undertakings. The ordinarily realized Digital Marketing Agency in India web-based promoting program that common computerized Digital Marketing Company in India can offer to its clients are Search Engine Optimization, Paid Marketing, Digital Marketing Company in Chennai