<?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>Richard Shepherd &#187; json</title>
	<atom:link href="http://www.richardshepherd.com/tag/json/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.richardshepherd.com</link>
	<description>Making the web. Better looking.</description>
	<lastBuildDate>Fri, 04 Jun 2010 18:03:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to use jQuery with a JSON Flickr feed to display photos</title>
		<link>http://www.richardshepherd.com/how-to-use-jquery-with-a-json-flickr-feed-to-display-photos/</link>
		<comments>http://www.richardshepherd.com/how-to-use-jquery-with-a-json-flickr-feed-to-display-photos/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 22:43:29 +0000</pubDate>
		<dc:creator>richardshepherd</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Flickr]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.growlingranger.com/?p=427</guid>
		<description><![CDATA[So today I needed to display photos from our Flickr Group without using php or asp.net. I have a WordPress widget that does this job nicely, but no PHP was allowed. Thank goodness for jQuery! jQuery has a handy little function which allows us to load JSON data using an HTTP GET request, and then [...]]]></description>
			<content:encoded><![CDATA[<p>So today I needed to display photos from our Flickr Group without using php or asp.net. I have a WordPress widget that does this job nicely, but no PHP was allowed.</p>
<p>Thank goodness for jQuery! jQuery has a handy little function which allows us to load JSON data using an HTTP GET request, and then process the contents as an array. And with some natty CSS to square things up as a grid, I have nine random (ish) photos from our Flickr group displaying in a nice 3&#215;3 grid.</p>
<p><a href="http://www.richardshepherd.com/flickr.html" target="_blank">See it in action</a>.</p>
<p>The full commented code is at the bottom of this post, but let&#8217;s go through it bit by bit.<span id="more-427"></span></p>
<h2>What is JSON?</h2>
<p>Since we&#8217;re talking about Flickr, let&#8217;s use they&#8217;re definition:</p>
<p><em>&#8220;JSON, or JavaScript Object Notation, is a simple machine-readable data-interchange format, which makes constructing API applications in JavaScript easy (though it can be used from other languages too!). &#8220;</em></p>
<p>Which means what exactly? Well, it&#8217;s a lot like any other feed or data &#8211; like an RSS feed, for example. A typical Flick JSON response might look like this:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Talk On Travel Pool&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;link&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.flickr.com/groups/talkontravel/pool/&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;description&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Travel and vacation photos from around the world, featured on the Talk On Travel blog. Join us in sharing your favourite destinations, your top hot spots, and the places to avoid. &quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;modified&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;2009-02-02T11:10:27Z&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;generator&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.flickr.com/&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;items&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;View from the hotel&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;link&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.flickr.com/photos/33112458@N08/3081564649/in/pool-998875@N22&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;media&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;m&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;http://farm4.static.flickr.com/3037/3081564649_4a6569750c_m.jpg&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;date_taken&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;2008-12-04T04:43:03-08:00&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;description&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&lt;p&gt;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>http://www.flickr.com/people/33112458@N08/<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt; Talk On Travel&lt;<span style="color: #000099; font-weight: bold;">\/</span>a&gt; has added a photo to the pool:&lt;<span style="color: #000099; font-weight: bold;">\/</span>p&gt; &lt;p&gt;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>http:// www.flickr.com/photos/33112458@N08/3081564649/<span style="color: #000099; font-weight: bold;">\&quot;</span> title=<span style="color: #000099; font-weight: bold;">\&quot;</span>View from the hotel<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt; &lt;img src=<span style="color: #000099; font-weight: bold;">\&quot;</span>http://farm4.static.flickr.com/3037/3081564649_4a6569750c_m.jpg<span style="color: #000099; font-weight: bold;">\&quot;</span> width=<span style="color: #000099; font-weight: bold;">\&quot;</span>240<span style="color: #000099; font-weight: bold;">\&quot;</span> height=<span style="color: #000099; font-weight: bold;">\&quot;</span>180<span style="color: #000099; font-weight: bold;">\&quot;</span> alt=<span style="color: #000099; font-weight: bold;">\&quot;</span>View from the hotel<span style="color: #000099; font-weight: bold;">\&quot;</span> /&gt;&lt;<span style="color: #000099; font-weight: bold;">\/</span>a&gt;&lt;<span style="color: #000099; font-weight: bold;">\/</span>p&gt; &quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;published&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;2008-12-04T12:43:03Z&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;author&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;nobody@flickr.com (Talk On Travel)&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;author_id&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;33112458@N08&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;tags&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;spain dolphins tenerife canaries lagomera aqualand playadelasamericas junglepark losgigantos loscristines talkontravel&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<p>Which is not altogether too hard to digest. <a href="http://api.flickr.com/services/feeds/groups_pool.gne?id=998875@N22&amp;lang=en-us&amp;format=json&amp;jsoncallback=?" target="_blank">To see the whole feed from Talk On Travel have a look here</a>.</p>
<p>All we need to do is get that feed in a way our javascript can use, and then use it. Enter, stage left, jQuery&#8230;</p>
<h2>Create our object and fill it with data</h2>
<p>So how can jQuery help us out? With the handy <strong>$.getJSON(url,  [data], [callback])</strong> command.</p>
<p>It&#8217;s simpliest method would be something like:</p>
<div class="codecolorer-container javascript default" style="border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.example.com/yourjsonfeed.json&quot;</span><span style="color: #339933;">,</span> functionName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Which would grab the info from a json feed at <em>www.example.com/yourjsonfeed.json</em> and then execute a function called <em>functionName</em>.</p>
<p>Then, you&#8217;d have to create a function like so:</p>
<div class="codecolorer-container javascript default" style="border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> functionName<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>In which <em>data</em> is automatically populated with the json feed. Nice, eh?</p>
<p>So now we have our <em>data</em> object, we need to know how to grab stuff out of it. Again, this is pretty easy stuff. We use some dot syntax like:</p>
<div class="codecolorer-container javascript default" style="border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">data.<span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">media</span>.<span style="color: #660066;">m</span></div></td></tr></tbody></table></div>
<p>Which says&#8230; in <em>data,</em> go to (the first) <em>item</em>, then go to the <em>media</em> bit and grab the <em>m</em> bit.</p>
<p>Scroll back to the top of this page, and in our example you&#8217;ll see that this would be &#8216;http://farm4.static.flickr.com/3037/3081564649_4a6569750c_m.jpg&#8217;.</p>
<p>You can probably work out then what <em>item.title</em> and <em>item.link</em> would give us. Armed with that, you can get any information you need from this feed. All we have to do now is display it!</p>
<h2>Getting this feed onto the screen</h2>
<p>Does that make sense so far? Hope so. If you get lost, check out the excellent jQuery documentation &#8211; the links for which are at the bottom of this post.</p>
<p>Let&#8217;s sew it all together in a really simple function that just cycles through all the images in the feed. By default, Flickr returns 20 images.</p>
<p>As I&#8217;ve commented the code, you should be able to work it out from here&#8230;</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Our very special jQuery JSON fucntion call to Flickr, gets details</span><br />
<span style="color: #006600; font-style: italic;">// of the most recent 20 images</span><br />
<br />
$.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://api.flickr.com/services/feeds/groups_pool.gne?id=<br />
998875@N22〈=en-us&amp;amp;format=json&amp;amp;jsoncallback=?&quot;</span><span style="color: #339933;">,</span> displayImages<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> displayImages<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Start putting together the HTML string</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> htmlString <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Now start cycling through our array of Flickr photo details</span><br />
&nbsp; &nbsp; $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">items</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// I only want the ickle square thumbnails</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> sourceSquare <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">media</span>.<span style="color: #660066;">m</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;_m.jpg&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;_s.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Here's where we piece together the HTML</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; htmlString <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;li&gt;&lt;a href=&quot;'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">link</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot; target=&quot;_blank&quot;&gt;'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; htmlString <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;img title=&quot;'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">title</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot; src=&quot;'</span> <span style="color: #339933;">+</span> sourceSquare<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; htmlString <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&quot; alt=&quot;'</span><span style="color: #339933;">;</span> htmlString <span style="color: #339933;">+=</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">title</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot; /&gt;'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; htmlString <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;/a&gt;&lt;/li&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; '</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Pop our HTML in the #images DIV</span><br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#images'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>htmlString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Close down the JSON function call</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<h2>But those aren&#8217;t my photos!?</h2>
<p>I know, I know. Have a look at that feed address once more&#8230;</p>
<p><em>http://api.flickr.com/services/feeds/groups_pool.gne?id=998875@N22&amp;lang=en-us&amp;format=json&amp;jsoncallback=?</em></p>
<p>Spot the <em>998875@N22 </em>bit? Well that&#8217;s the Flickr user ID for Talk On Travel, another blog that I look after. You need to put your own ID in that string.</p>
<p>I recommend using  http://idgettr.com/ to find out easily!</p>
<h2>Over to you</h2>
<p>That&#8217;s a simple introduction to JSON and Flickr, and I hope it puts you on the right path.</p>
<p>For more details on JSON and jQuery, and with another example of how to get photos from a Flickr JSON feed, check out <a href="http://docs.jquery.com/Ajax/jQuery.getJSON" target="_blank">http://docs.jquery.com/Ajax/jQuery.getJSON</a>.</p>
<p>To read up on the Flickr JSON object, head over to <a href="http://www.flickr.com/services/api/response.json.html" target="_blank">http://www.flickr.com/services/api/response.json.html</a>.</p>
<p>You can see my code in action at <a href="http://www.richardshepherd.com/flickr.html" target="_blank">http://www.richardshepherd.com/flickr.html</a>. Check out the source code to see what it&#8217;s doing.</p>
<p>Obviously, you can do a lot more with this, but it&#8217;s just a basic implementation to get you started.</p>
<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardshepherd.com/how-to-use-jquery-with-a-json-flickr-feed-to-display-photos/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
	</channel>
</rss>
