Thursday, November 26, 2009

Why I now Prefer JSON to RSS/XML

This might mainly be a “call-out” to Dave Winer, since he is continually
attempting to push RSS for all sorts of data that should be inherited around the web – although, he has
stated before that he likes JSON – I think – My memory is not so good, even my girlfriend has been
complaining about it.

Either way, I’m just going to take a small amount of time here to
list why I no longer like XML and RSS for portable data.

It’s too low-level.


XML and RSS are a markup language, of course.  It’s very low-level code, and is generally
a pain to parse in any language.  Which is why I prefer JSON.


It’s high-level code.

JSON is a very simple object – It’s basically an array of
data.  Here is some XML vs. JSON:

XML


style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px;
font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none;
color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<data            style="color: #0000ff">>




 <user id="876232">




 <name>User Name            style="color: #0000ff"></name            style="color: #0000ff">>




 <screenName>User            style="color: #0000ff"></screenName            style="color: #0000ff">>




 </user>




</data>




JSON



style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom:
4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%;
padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr;
max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right:
silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px;
font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none;
color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">

"user":[            style="color: #006080">"id":876232,"name":            style="color: #006080">"User Name","screenName":            style="color: #006080">"User"]






Even
though you can get similar results by making id its own tag in the XML, its still difficult to parse. 
Where as with JSON, the data is already formatted and easily accessible.  Especially in my favorite
language, PHP.



id="codeSnippetWrapper">

// For Json




 $json = json_decode($data,TRUE); // Decodes JSON
and makes it a true array



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> $id = $json["user"][ style="color: #006080">"id"];



 $name = $json["user"][            style="color: #006080">"name"];




// For XML




 $xml = new SimpleXML($data);




 $id = $xml->user["id"];




 $name = $xml->user->name;






The
two are very close, but in the end JSON is more simplistic – because its such a high-level language.

No comments:

Post a Comment