<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Robertdot, A Web Design Blog &#187; JSON</title>
	<atom:link href="http://robertdot.org/tags/json/feed/" rel="self" type="application/rss+xml" />
	<link>http://robertdot.org</link>
	<description>A web design blog for designers that develop or developers that design.</description>
	<lastBuildDate>Fri, 05 Mar 2010 15:59:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JSON, An Alternative to XML in AJAX</title>
		<link>http://robertdot.org/2006/07/18/json-an-alternative-to-xml-in-ajax/</link>
		<comments>http://robertdot.org/2006/07/18/json-an-alternative-to-xml-in-ajax/#comments</comments>
		<pubDate>Tue, 18 Jul 2006 14:49:55 +0000</pubDate>
		<dc:creator>Robert</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://robertdot.org/2006/07/18/json-an-alternative-to-xml-in-ajax/</guid>
		<description><![CDATA[
 I admit it.  I like Ajax.  Sure it&#8217;s a dumb buzzword.  But, as you&#8217;ll recall, in How To Make an Ajax Chat Room, I said something like, It just turns out XML is slightly more usable for complex stuff and that synchronous requests defeat the point.  Though Ajat have many [...]]]></description>
			<content:encoded><![CDATA[<p>
 I admit it.  I like <abbr title="Asynchronous Javascript and XML">Ajax</abbr>.  Sure it&#8217;s a dumb buzzword.  But, as you&#8217;ll recall, in <a href="/2006/06/23/how-to-make-an-ajax-chat-room/"><cite>How To Make an Ajax Chat Room</cite></a>, I said something like, <q>It just turns out XML is slightly more usable for complex stuff and that synchronous requests defeat the point.  Though <q><abbr title="Asynchronous JavaScript and Text">Ajat</abbr></q> have many uses.</q>  The point I was trying to make is that sometimes sending text back instead of <abbr title="Extensible Markup Language">XML</abbr> is nice.  For example, I can send back a <samp>1</samp> if the action was successful.  But sometimes that isn&#8217;t enough.  Sometimes, text and XML are both the wrong tools for the job.  That&#8217;s where <abbr title="Javascript Object Notation">JSON</abbr> comes in.
</p>
<h2>The Problem With XML</h2>
<p>
 The problem with XML is that it is a bitch to work with.  XML files can get big if the file uses meaningful metadata.  To boot, they are a pain in the ass to create when all you really want to do is send some simple data that is too complex for regular text.  Then, when it comes time to use the XML in JavaScript, one has to do lots of <abbr title="Document Object Model">DOM</abbr> stuff just to get to the data.
</p>
<p>
 Since I don&#8217;t do much PHP work, I don&#8217;t have to deal with the creation of the XML files.  I do, however, have to access the data from the DOM.  I got so irritated by it, that I wrote a function in my Ajax class that will convert simple XML files in to Javascript associative arrays.  It turns out I was taking an unneeded step.
</p>
<h2>The Problem With Text</h2>
<p>
 As I mentioned, sending text back is often useful.  Since <code>XMLHttpRequest</code> is smart enough to recognize the content type of the returned code, I often send back errors as text and do something similar to this to display the error: <code>if(!ajax_obj.responseXML){alert(ajax_obj.responseText);}</code>.  Sometimes, however, I return <samp>1</samp> to alert success and a string error message to give an error.  While I&#8217;ve made this standard practice, it can get confusing when different Ajax requests get different styles of messages returned.
</p>
<p>
 Additionally, making things more complex, sometimes sending a single raw value back doesn&#8217;t require an XML wrapper.  For the sake of speeding up the request, the value is sent as text.  This mucks with the paradigm I mentioned before.
</p>
<h2>JSON</h2>
<p>
 XML is nice for documents.  It&#8217;s a pain to work with as a data exchange format.  Text is simpler, but can be inconsistent, often not providing enough useful data.  That is where JSON picks up.
</p>
<p>
 JSON, short for JavaScript Object Notation, is exactly what it says it is.  It is the syntax used to create objects in JavaScript (and apparently Python, too).  If you are curious, it would look like this in Javascript:
</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">my_object <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;item1&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;item2&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span>my_object<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;item1&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Outputs &quot;value&quot;</span></pre></td></tr></table></div>

<p>
 You can nest objects inside objects, too.
</p>
<h2>Using JSON Instead of XML</h2>
<p>
 Outputting XML from <abbr title="PHP Hypertext Preprocessor">PHP</abbr> is straight forward.  JSON is similarly straight forward.  The PHP script simply dumps out text similar to the object literal notation shown above (without the variable assignment).  The request will be stored in the <code>.responseText</code>
</p>
<p>
 When an <code>XMLHttpRequest</code> is sent to the server and XML is received, the XML is automagically parsed into a variable as a DOM object.  JSON requires an extra step to assign the JSON object to a variable that you can use in your script.
</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">my_object <span style="color: #339933;">=</span>  <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;(&quot;</span><span style="color: #339933;">+</span>request_object.<span style="color: #660066;">responseText</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>
 It&#8217;s pretty easy.  If integrated into a class, the <code>eval</code> could be done on-the-fly.
</p>
<h2>Freddy &#8212; I mean, XML vs JSON</h2>
<p>
 I&#8217;m still a JSON newb.  I&#8217;ve done a pretty good deal of Ajax work, though.  I&#8217;ve seen some very irritating stuff.  For example, when fetching a few hundred records in XML format, the file can get large and cause the browser to <q>freeze</q> while the XML is parsed.  Since JSON would be no different from executing Javascript code, it should avoid the crawl.
</p>
<p>
 Unless XML tags are really light weight (which defeats the whole metadata concept of XML), the files can get really big.  JSON doesn&#8217;t need as much tagging, so file sizes can be smaller.
</p>
<p>
 XML is pretty simple to write.  JSON is pretty simple to write.  XML must be well-formed.  JSON must be syntactically correct.  JSON just happens to require less typing than XML.
</p>
<p>
 I&#8217;m throwing this bonus in, even though it isn&#8217;t specific to Javascript:  Not all languages have nice support for XML.  PHP 4, for example, will do XML; it is just really hard.  There are quite a few JSON interpreters for various languages and JavaScript and Python have it built-in.  With XML, one has to learn how to use the XML library.  With JSON, all one needs to know is how to work with normal objects in that language.  Specifically with Javascript, you don&#8217;t need to learn DOM scripting to work with JSON.  You just need to know how to use associative arrays.
</p>
<h2>Real World Testing</h2>
<p>
 I did a simple test.  I wrote a PHP file that creates an XML file with 300 values and a JSON file with 300 values.  I later modified the loop to 1000 values to see if an article I read was true about Ajax breaking down exponentially.  It&#8217;s basically true, by the way.
</p>
<p>
 To be fair, I had already written the code, then modified it to run on JSON.  For that to work, I had to make the JSON output match what would show after my XML file was turned into an object.  The XML file was already optimized to be very small.  So, while that might give some size overhead to my JSON (since it had to mimmic nodes), it also gives some parsing overhead to my Ajax (since it was converted to an object before use).  What I mean is that the code I used could be more efficient (and I might re-write it eventually).
</p>
<p>
 The Javascript reads the XML or JSON file.  It uses the information to populate a <code>select</code>.  I uploaded it to a server across the country from where I am and I ran the files.  The server gets spikes of heavy use, so the results may be skewed.
</p>
<p>
 At 300 records, I couldn&#8217;t really tell a difference between the JSON and the AJAX.  Early tests at 1000 records, it was debatable.  JSON was at least as fast as XML (usually around 7 seconds from refresh until was complete).  Some requests were far faster on JSON (2 seconds instead of 7).  Later tests revealed a much larger variation, with XML taking 40 seconds and JSON taking only 15.
</p>
<h2>The Golden Fleece</h2>
<p>
 I like JSON for what it is.  It&#8217;s very capable and much lighter than XML, both in terms of parsing and in terms of file size.  It is a capable alternative to XML and has quite a few undeniable upsides.  I&#8217;ll probably do further testing before rewriting all my legacy code, but I will be using JSON in the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://robertdot.org/2006/07/18/json-an-alternative-to-xml-in-ajax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
