JSON, XML, REST and SOAP

I am new to Web related technologies and did some reading to get familiar with the Web technologies used with Opendaylight. Following blog covers some of these.

XML vs JSON:

Both are formats to represent and transfer data and used extensively for Web services.

XML:

  • Can hold any type of data.
  • Extensible and supports markup.
  • Human readable, difficult for computers to parse.

JSON:

  • Lightweight.
  • Does not support markup.
  • Represented as key, value pair. Not easily human readable, very easy for clients to parse data.

JSON, XML Example:

Following example shows the flow output collected from Opendaylight application in XML and JSON format and sample python program to parse it. 

JSON output:(flow.json)

{
"flowConfig": [
{
"installInHw": "true",
"name": "NORMAL",
"node": {
"id": "00:00:00:00:00:00:00:01",
"type": "OF"
},
"priority": "0",
"actions": [
"HW_PATH"
]
},
{
"installInHw": "true",
"name": "PuntLLDP",
"node": {
"id": "00:00:00:00:00:00:00:01",
"type": "OF"
},
"priority": "1000",
"etherType": "0x88CC",
"actions": [
"CONTROLLER"
]
},
{
"installInHw": "true",
"name": "__Punt ARP__",
"node": {
"id": "00:00:00:00:00:00:00:01",
"type": "OF"
},
"priority": "1",
"etherType": "0x806",
"actions": [
"CONTROLLER"
]
},
{
"installInHw": "true",
"name": "__Punt LLDP__",
"node": {
"id": "00:00:00:00:00:00:00:01",
"type": "OF"
},
"priority": "1",
"etherType": "0x88CC",
"actions": [
"CONTROLLER"
]
}
]
}

Python program to parse JSON:

import json

json_data=open('flow.json').read()

tree = json.loads(json_data)

flowarr = tree['flowConfig']
for flow in flowarr:
print flow

Output of above python program:

{u'node': {u'type': u'OF', u'id': u'00:00:00:00:00:00:00:01'}, u'priority': u'0', u'installInHw': u'true', u'name': u'NORMAL', u'actions': [u'HW_PATH']}
{u'node': {u'type': u'OF', u'id': u'00:00:00:00:00:00:00:01'}, u'installInHw': u'true', u'name': u'PuntLLDP', u'etherType': u'0x88CC', u'actions': [u'CONTROLLER'], u'priority': u'1000'}
{u'node': {u'type': u'OF', u'id': u'00:00:00:00:00:00:00:01'}, u'installInHw': u'true', u'name': u'__Punt ARP__', u'etherType': u'0x806', u'actions': [u'CONTROLLER'], u'priority': u'1'}
{u'node': {u'type': u'OF', u'id': u'00:00:00:00:00:00:00:01'}, u'installInHw': u'true', u'name': u'__Punt LLDP__', u'etherType': u'0x88CC', u'actions': [u'CONTROLLER'], u'priority': u'1'}

XML output:(flow.xml)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<list>
<flowConfig>
<installInHw>true</installInHw>
<name>NORMAL</name>
<node>
<id>00:00:00:00:00:00:00:01</id>
<type>OF</type>
</node>
<priority>0</priority>
<actions>HW_PATH</actions>
</flowConfig>
<flowConfig>
<installInHw>true</installInHw>
<name>PuntLLDP</name>
<node>
<id>00:00:00:00:00:00:00:01</id>
<type>OF</type>
</node>
<priority>1000</priority>
<etherType>0x88CC</etherType>
<actions>CONTROLLER</actions>
</flowConfig>
<flowConfig>
<installInHw>true</installInHw>
<name>__Punt ARP__</name>
<node>
<id>00:00:00:00:00:00:00:01</id>
<type>OF</type>
</node>
<priority>1</priority>
<etherType>0x806</etherType>
<actions>CONTROLLER</actions>
</flowConfig>
<flowConfig>
<installInHw>true</installInHw>
<name>__Punt LLDP__</name>
<node>
<id>00:00:00:00:00:00:00:01</id>
<type>OF</type>
</node>
<priority>1</priority>
<etherType>0x88CC</etherType>
<actions>CONTROLLER</actions>
</flowConfig>
</list>

Python program to read XML:

import xml.etree.cElementTree as ET

tree = ET.ElementTree(file='flow.xml')

for elem in tree.iter():
print elem.tag, elem.attrib, elem.text

Output of above python program:

list {} 

flowConfig {}

installInHw {} true
name {} NORMAL
node {}

id {} 00:00:00:00:00:00:00:01
type {} OF
priority {} 0
actions {} HW_PATH
flowConfig {}

installInHw {} true
name {} PuntLLDP
node {}

id {} 00:00:00:00:00:00:00:01
type {} OF
priority {} 1000
etherType {} 0x88CC
actions {} CONTROLLER
flowConfig {}

installInHw {} true
name {} __Punt ARP__
node {}

id {} 00:00:00:00:00:00:00:01
type {} OF
priority {} 1
etherType {} 0x806
actions {} CONTROLLER
flowConfig {}

installInHw {} true
name {} __Punt LLDP__
node {}

id {} 00:00:00:00:00:00:00:01
type {} OF
priority {} 1
etherType {} 0x88CC
actions {} CONTROLLER

As can be seen from above example, its much easier to parse JSON output and treat data as a list.

REST vs SOAP:

Both are mechanisms for a Web service to interact with clients.

SOAP:

  • SOAP is a protocol specification that uses XML as the message format.
  • Its highly extensible.
  • Services can be discovered automatically.
  • XML parsing is difficult for web clients, needs separate tools.

REST:

  • REST is an architectural style that represents everything as a resource and resources can be accessed using standard http interfaces and URIs.
  • Lightweight when compared to SOAP.
  • Data format can be either csv, xml or json.
  • Since json formatting is built-in to programming languages like Python, Ruby, there are no separate tools needed to parse REST results.

REST example:

Request URL:
http://localhost:8080/controller/nb/v2/switchmanager/default/nodes
<list>
   <nodeProperties>
      <node>
         <id>00:00:00:00:00:00:00:02</id>
         <type>OF</type>
      </node>
      <properties>
         <tables>
            <value>-1</value>
         </tables>
         <description>
            <value>Switch2</value>
         </description>
         <actions>
            <value>4095</value>
         </actions>
         <macAddress>
            <value>00:00:00:00:00:02</value>
         </macAddress>
         <capabilities>
            <value>199</value>
         </capabilities>
         <timeStamp>
            <value>1377291227877</value>
            <name>connectedSince</name>
         </timeStamp>
         <buffers>
            <value>256</value>
         </buffers>
      </properties>
   </nodeProperties>
</list>

Response body in JSON:
{
"nodeProperties":[
{
"node":{
"id":"00:00:00:00:00:00:00:02",
"type":"OF"
},
"properties":{
"tables":{
"value":"-1"
},
"description":{
"value":"None"
},
"actions":{
"value":"4095"
},
"macAddress":{
"value":"00:00:00:00:00:02"
},
"capabilities":{
"value":"199"
},
"timeStamp":{
"value":"1377291227877",
"name":"connectedSince"
},
"buffers":{
"value":"256"
}
}
}
]
}
  • Opendaylight as a web service exposes REST apis for external applications to interact with Opendaylight.
  • In the above example, switchmanager is module, resource is node and we are using http get request to get list of nodes. We can get the response in either xml, json or csv format. Other http verbs like put, delete, post are also supported in REST.
  • Getting into more details on the above json response, “nodeProperties” is the key and the value is a list of nodes with each node having a set of properties.
  • In xml format, in the above example, we see the node list. In json format, in the above example, we see a list of nodes in key, value format and programming languages like Python can parse this data into lists automatically.

References:

One thought on “JSON, XML, REST and SOAP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s