Wednesday, April 22, 2009

XML vs. JSON - Size Comparison

I was wondering how compact is JSON comparing to XML. I grabbed an arbitrary XML from the web and convert it to an equivalent JSON format. It turns out that JSON is around two-third of the XML size.

XML: 596 bytes

JSON: 402 bytes

<?xml version="1.0"?>
<message>
<header>
<to>companyReceiver</to>
<from>companySender</from>
<type>saveInvoice</type>
</header>
<info>
<saveInvoice>
<invoice date="01-20-2000" number="123">
<address country="US">
<name>John Smith</name>
<street>123 George St.</street>
<city>Mountain View</city>
<state>CA</state>
<zip>94041</zip>
</address>
<billTo country="US">
<name>Company A</name>
<street>100 Main St.</street>
<city>Washington</city>
<state>DC</state>
<zip>20015</zip>
</billTo>
<items>
<item number="1">
<name>IBM A20 Laptop</name>
<quantity>1</quantity>
<USPrice>2000.00</USPrice>
</item>
</items>
</invoice>
</saveInvoice>
</info>
</message>
 
{
header:{
to:"companyReceiver",
from:"companySender",
type:"saveInvoice”
},
info:{
saveInvoice:{
invoice:{date:"01-20-2000,number:"123",
address:{country:"US",
name:"John Smith",
street:"123 George St.",
city:"Mountain View",
state:"CA",
zip:"94041"
},
billTo:{country="US",
name:"Company A",
street:"100 Main St.",
city:"Washington",
state:"DC",
zip:"20015"
},
items:[
{
name:"IBM A20 Laptop",
quantity:1,
USPrice:2000.00
}
]
}
}
}
}