<?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>Silas Sewell - Universal Feed Parser</title>
        <atom:link href="http://www.silassewell.com/blog/tag/universal-feed-parser/rss2.xml" rel="self" type="application/rss+xml" />
        <link>http://www.silassewell.com/blog/tag/universal-feed-parser</link>
        <description>Infrastructure Development</description>
        <lastBuildDate>Sat, 08 Jan 2011 00:00:00 GMT</lastBuildDate>
        <generator>http://www.silassewell.com/</generator>
        <language>en</language>
        <sy:updatePeriod>hourly</sy:updatePeriod>
        <sy:updateFrequency>1</sy:updateFrequency>

        <item>
            <title>Universal Feed Parser to JSON</title>
            <link>http://www.silassewell.com/blog/2008/05/05/universal-feed-parser-json/</link>
            <pubDate>Mon, 05 May 2008 00:00:00 GMT</pubDate>
            <dc:creator>silas</dc:creator>
            <category><![CDATA[JSON]]></category><category><![CDATA[Programming]]></category><category><![CDATA[Python]]></category><category><![CDATA[Universal Feed Parser]]></category>
            <guid isPermaLink="true">http://www.silassewell.com/blog/2008/05/05/universal-feed-parser-json/</guid>
            <description><![CDATA[<p>My attempt at wrangling a Universal Feed Parser object into JSON.</p>

<pre><code class="prettyprint">import feedparser

def json_make_normal(obj):
    if type(obj) in [str, unicode, int, float, bool, dict, set, list, tuple]:
        return obj
    try: return dict(obj)
    except: pass
    try: return list(obj)
    except: pass
    return None

def json_handle(obj):
    obj = json_make_normal(obj)
    if type(obj) in [str, unicode]:
        obj = obj.replace('\\', '\\\\')
        obj = obj.replace('"', '\\"')
        obj = obj.replace('\b', '\\\b')
        obj = obj.replace('\f', '\\\f')
        obj = obj.replace('\n', '\\\n')
        obj = obj.replace('\r', '\\\r')
        obj = obj.replace('\t', '\\\t')
        return '"%s"' % obj
    elif type(obj) in [int, float]:
        return obj
    elif type(obj) is bool:
        if obj: return 'true'
        else: return 'false'
    elif type(obj) is type(None):
        return 'null'
    elif type(obj) is dict:
        temp = ''
        for key in obj.keys():
            temp += '%s:%s, ' % (json_handle(key), json_handle(obj[key]))
        return '{%s}' % temp[:-2]
    elif type(obj) in [set, list, tuple]:
        temp = ''
        for value in obj:
            temp += '%s, ' % json_handle(value)
        return '[%s]' % temp[:-2]
    return 'null'

data = feedparser.parse('http://digg.com/rss/index.xml')

print json_handle(data)
</code></pre>

<p>Link: <a href="http://www.silassewell.com/blog/2008/05/05/universal-feed-parser-json/feed_parser_to_json.py" title="feed parser to json">feed_parser_to_json.py</a></p>

<p><strong>Update:</strong> According to <a href="http://code.google.com/u/john.paulett/" title="John Paulett">John Paulett</a> <a href="http://code.google.com/p/jsonpickle/" title="jsonpickle">jsonpickle</a> now supports Universal Feed Parser objects. Thanks for letting me know!</p>]]></description>
        </item>

    </channel>
</rss>
