<?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>beer planet &#187; south park</title>
	<atom:link href="http://beerpla.net/tag/south-park/feed/" rel="self" type="application/rss+xml" />
	<link>http://beerpla.net</link>
	<description>where things have nothing to do with beer - tutorials, tips, how-tos, thoughts, hacks, and other techy nonsense</description>
	<lastBuildDate>Tue, 27 Jul 2010 04:25:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<atom:link rel='hub' href='http://beerpla.net/?pushpress=hub'/>
		<item>
		<title>Parsing JSON In Perl By Example &#8211; SouthParkStudios.com South Park Episodes</title>
		<link>http://beerpla.net/2008/03/27/parsing-json-in-perl-by-example-southparkstudioscom-south-park-episodes/</link>
		<comments>http://beerpla.net/2008/03/27/parsing-json-in-perl-by-example-southparkstudioscom-south-park-episodes/#comments</comments>
		<pubDate>Thu, 27 Mar 2008 16:51:36 +0000</pubDate>
		<dc:creator>Artem Russakovskii</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[parse]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[south park]]></category>
		<guid isPermaLink="false">http://beerpla.net/2008/03/27/parsing-json-in-perl-by-example-southparkstudioscom-south-park-episodes/</guid>
		<description><![CDATA[<p>In this tutorial, I&#039;ll show you how to parse <a href="http://search.cpan.org/dist/JSON/">JSON</a> using Perl. As a fun example, I&#039;ll use the new <a href="http://www.southparkstudios.com">SouthParkStudios.com</a> site released earlier this week, which contains full legal episodes of South Park. I guess the TV companies are finally getting a clue about what users want.</p>
<p>I will parse the first season&#039;s JSON and pull out information about individual episodes (like title, description, air date, etc) from <a href="http://www.southparkstudios.com/includes/utils/proxy_feed.php?html=season_json.jhtml%3fseason=1">http://www.southparkstudios.com/includes/utils/proxy_feed.php?html=season_json.jhtml%3fseason=1</a>. Feel free to replace &#039;1&#039; with any valid season number.</p>
<p>Here&#039;s a short snippet of the JSON:</p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9</pre></td></tr></table></div><p>...<div class=clear></div> <a href="http://beerpla.net/2008/03/27/parsing-json-in-perl-by-example-southparkstudioscom-south-park-episodes/" class="read_more"><div class=excerpt-end>Read the rest of this article &#187;</div></a></p>]]></description>
			<content:encoded><![CDATA[<p>In this tutorial, I&#039;ll show you how to parse <a href="http://search.cpan.org/dist/JSON/">JSON</a> using Perl. As a fun example, I&#039;ll use the new <a href="http://www.southparkstudios.com">SouthParkStudios.com</a> site released earlier this week, which contains full legal episodes of South Park. I guess the TV companies are finally getting a clue about what users want.</p>
<p>I will parse the first season&#039;s JSON and pull out information about individual episodes (like title, description, air date, etc) from <a href="http://www.southparkstudios.com/includes/utils/proxy_feed.php?html=season_json.jhtml%3fseason=1">http://www.southparkstudios.com/includes/utils/proxy_feed.php?html=season_json.jhtml%3fseason=1</a>. Feel free to replace &#039;1&#039; with any valid season number.</p>
<p>Here&#039;s a short snippet of the JSON:</p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code"><pre>{
season:{
&nbsp;
episode:[
&nbsp;
{
title:'Cartman Gets an Anal Probe',
description:'While the boys are waiting for the school bus, Cartman explains the odd nightmare he had the previous night involving alien visitors.',
thumbnail:'http://www.southparkstudios.com/includes/utils/proxy_resizer.php?image=/images/south_park/episode_thumbnails/s01e01_480.jpg&amp;width=55&amp;quality=100',
thumbnail_larger:'http://www.southparkstudios.com/includes/utils/proxy_resizer.php?image=/images/south_park/episode_thumbnails/s01e01_480.jpg&amp;width=63&amp;quality=100',
thumbnail_190:'http://www.southparkstudios.com/includes/utils/proxy_resizer.php?image=/images/south_park/episode_thumbnails/s01e01_480.jpg&amp;width=190&amp;quality=100',
id:'103511',
airdate:'08.13.97',
episodenumber:'101',
available:'true',
when:'08.13.97'
}
&nbsp;
,
&nbsp;
{
title:'Weight Gain 4000',
description:'When Cartman\'s environmental essay wins a national contest, America\'s sweetheart, Kathie Lee Gifford, comes to South Park to present the award.',
thumbnail:'http://www.southparkstudios.com/includes/utils/proxy_resizer.php?image=/images/south_park/episode_thumbnails/s01e02_480.jpg&amp;width=55&amp;quality=100',
thumbnail_larger:'http://www.southparkstudios.com/includes/utils/proxy_resizer.php?image=/images/south_park/episode_thumbnails/s01e02_480.jpg&amp;width=63&amp;quality=100',
thumbnail_190:'http://www.southparkstudios.com/includes/utils/proxy_resizer.php?image=/images/south_park/episode_thumbnails/s01e02_480.jpg&amp;width=190&amp;quality=100',
id:'103516',
airdate:'08.20.97',
episodenumber:'102',
available:'true',
when:'08.20.97'
}
&nbsp;
...
&nbsp;
]
}
}</pre></td></tr></table></div>
<p>
  <br />Before you can parse JSON, you need to have a few libraries. Install them using CPAN, for example: </p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre>cpan
install JSON
install JSON::XS
install WWW::Mechanize # my favorite library for browsing</pre></td></tr></table></div>
<p>Now <a href="http://beerpla.net/svn/public/Perl/Parsing.JSON/southpark.json.pl">the script</a>. </p>
<p></p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td class="code"><pre>#!/usr/bin/perl -w
# $Rev: 11 $
# $Author: artem $
# $Date: 2009-05-23 23:09:47 -0700 (Sat, 23 May 2009) $
&nbsp;
use strict;
use WWW::Mechanize;
use JSON -support_by_pp;
&nbsp;
fetch_json_page(&quot;http://www.southparkstudios.com/includes/utils/proxy_feed.php?html=season_json.jhtml%3fseason=1&quot;);
&nbsp;
sub fetch_json_page
{
  my ($json_url) = @_;
  my $browser = WWW::Mechanize-&gt;new();
  eval{
    # download the json page:
    print &quot;Getting json $json_url\n&quot;;
    $browser-&gt;get( $json_url );
    my $content = $browser-&gt;content();
    my $json = new JSON;
&nbsp;
    # these are some nice json options to relax restrictions a bit:
    my $json_text = $json-&gt;allow_nonref-&gt;utf8-&gt;relaxed-&gt;escape_slash-&gt;loose-&gt;allow_singlequote-&gt;allow_barekey-&gt;decode($content);
&nbsp;
    # iterate over each episode in the JSON structure:
    my $episode_num = 1;
    foreach my $episode(@{$json_text-&gt;{season}-&gt;{episode}}){
      my %ep_hash = ();
      $ep_hash{title} = &quot;Episode $episode_num: $episode-&gt;{title}&quot;;
      $ep_hash{description} = $episode-&gt;{description};
      $ep_hash{url} = &quot;http://www.southparkstudios.com/episodes/&quot; . $episode-&gt;{id};
      $ep_hash{publish_date} = $episode-&gt;{airdate};
      $ep_hash{thumbnail_url} = $episode-&gt;{thumbnail_190} || $episode-&gt;{thumbnail_larger};
&nbsp;
      # print episode information:
      while (my($k, $v) = each (%ep_hash)){
        print &quot;$k =&gt; $v\n&quot;;
      }
      print &quot;\n&quot;;
&nbsp;
      $episode_num++;
    }
  };
  # catch crashes:
  if($@){
    print &quot;[[JSON ERROR]] JSON parser crashed! $@\n&quot;;
  }
}</pre></td></tr></table></div>
<p>Here&#039;s the output: </p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre>Getting json http://www.southparkstudios.com/includes/utils/proxy_feed.php?html=season_json.jhtml%3fseason=1
publish_date =&gt; 08.13.97
url =&gt; http://www.southparkstudios.com/episodes/103511
thumbnail_url =&gt; http://www.southparkstudios.com/includes/utils/proxy_resizer.php?image=/images/south_park/episode_thumbnails/s01e01_480.jpg&amp;width=190&amp;quality=100
title =&gt; Episode 1: Cartman Gets an Anal Probe
description =&gt; While the boys are waiting for the school bus, Cartman explains the odd nightmare he had the previous night involving alien visitors.
&nbsp;
publish_date =&gt; 08.20.97
url =&gt; http://www.southparkstudios.com/episodes/103516
thumbnail_url =&gt; http://www.southparkstudios.com/includes/utils/proxy_resizer.php?image=/images/south_park/episode_thumbnails/s01e02_480.jpg&amp;width=190&amp;quality=100
title =&gt; Episode 2: Weight Gain 4000
description =&gt; When Cartman's environmental essay wins a national contest, America's sweetheart, Kathie Lee Gifford, comes to South Park to present the award.
&nbsp;
...</pre></td></tr></table></div>
<p>Of particular interest here is the way JSON accepts settings:</p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre>my $json_text = $json-&gt;allow_nonref-&gt;utf8-&gt;relaxed-&gt;escape_slash-&gt;loose-&gt;allow_singlequote-&gt;allow_barekey-&gt;decode($content);</pre></td></tr></table></div>
<p>I found that these settings fix most of the crashes and incompatibilities while parsing various JSON pages.</p>
<p>Is there something I&#039;ve missed? Do you know a better way to parse JSON in Perl? Unclear about something. Don&#039;t hesitate to share in the comments.</p>
<div class="shr-bookmarks shr-bookmarks-expand">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Parsing+JSON+In+Perl+By+Example+-+SouthParkStudios.com+South+Park+Episodes+-+http://bit.ly/5mOJwd&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://beerpla.net/2008/03/27/parsing-json-in-perl-by-example-southparkstudioscom-south-park-episodes/&amp;t=Parsing+JSON+In+Perl+By+Example+-+SouthParkStudios.com+South+Park+Episodes" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://beerpla.net/2008/03/27/parsing-json-in-perl-by-example-southparkstudioscom-south-park-episodes/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-hackernews">
			<a href="http://news.ycombinator.com/submitlink?u=http://beerpla.net/2008/03/27/parsing-json-in-perl-by-example-southparkstudioscom-south-park-episodes/&amp;t=Parsing+JSON+In+Perl+By+Example+-+SouthParkStudios.com+South+Park+Episodes" rel="nofollow" class="external" title="Submit this to Hacker News">Submit this to Hacker News</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://beerpla.net/2008/03/27/parsing-json-in-perl-by-example-southparkstudioscom-south-park-episodes/&amp;title=Parsing+JSON+In+Perl+By+Example+-+SouthParkStudios.com+South+Park+Episodes" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://beerpla.net/2008/03/27/parsing-json-in-perl-by-example-southparkstudioscom-south-park-episodes/&amp;title=Parsing+JSON+In+Perl+By+Example+-+SouthParkStudios.com+South+Park+Episodes" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://beerpla.net/2008/03/27/parsing-json-in-perl-by-example-southparkstudioscom-south-park-episodes/&amp;title=Parsing+JSON+In+Perl+By+Example+-+SouthParkStudios.com+South+Park+Episodes" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://beerpla.net/2008/03/27/parsing-json-in-perl-by-example-southparkstudioscom-south-park-episodes/&amp;title=Parsing+JSON+In+Perl+By+Example+-+SouthParkStudios.com+South+Park+Episodes" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-mail">
			<a href="mailto:?subject=%22Parsing%20JSON%20In%20Perl%20By%20Example%20-%20SouthParkStudios.com%20South%20Park%20Episodes%22&amp;body=Link: http://beerpla.net/2008/03/27/parsing-json-in-perl-by-example-southparkstudioscom-south-park-episodes/ (sent via shareaholic)%0D%0A%0D%0A----%0D%0A In%20this%20tutorial%2C%20I%27ll%20show%20you%20how%20to%20parse%20JSON%20using%20Perl.%20As%20a%20fun%20example%2C%20I%27ll%20use%20the%20new%20SouthParkStudios.com%20site%20released%20earlier%20this%20week%2C%20which%20contains%20full%20legal%20episodes%20of%20South%20Park.%20I%20guess%20the%20TV%20companies%20are%20finally%20getting%20a%20clue%20about%20what%20users%20want.%20%20I%20will%20parse%20the%20first%20" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>
Similar Posts:<ul><li><a href="http://beerpla.net/2010/01/12/are-your-youtube-video-embeds-missing-the-full-screen-button-here-is-how-to-fix-it/" rel="bookmark" title="January 12, 2010">Are Your Youtube Video Embeds Missing The Full Screen Button? Here Is How To Fix It</a></li>
<li><a href="http://beerpla.net/2010/01/09/the-easiest-and-best-way-to-search-for-beautiful-wallpapers-in-your-specific-screen-resolution-and-size/" rel="bookmark" title="January 9, 2010">The Easiest (And Best?) Way To Search For Beautiful Wallpapers In Your Specific Screen Resolution And Size</a></li>
<li><a href="http://beerpla.net/2006/08/19/prison-break-season-2-returns-august-21st/" rel="bookmark" title="August 19, 2006">Prison Break &#8211; Season 2 Returns August 21st!</a></li>
<li><a href="http://beerpla.net/2010/01/15/follow-up-to-loading-css-and-js-conditionally/" rel="bookmark" title="January 15, 2010">Follow-up To Loading CSS And JS Conditionally</a></li>
<li><a href="http://beerpla.net/2007/08/04/watch-a-useful-linux-command-you-may-have-never-heard-of/" rel="bookmark" title="August 4, 2007">Watch &#8211; A Useful Linux Command You May Have Never Heard Of</a></li>
</ul><!-- Similar Posts took 15.733 ms --><p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://beerpla.net/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://beerpla.net/2008/03/27/parsing-json-in-perl-by-example-southparkstudioscom-south-park-episodes/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
