tags:

views:

1691

answers:

6

I am writing an RSS feed (for fun) and was looking at the spec here.

RSS is a dialect of XML. All RSS files must conform to the XML 1.0 specification, as published on the World Wide Web Consortium (W3C) website.

Obviously this means that I am not serving 'pure' RSS if I choose the JSON option. That said, if I conform to the rest of the spec, is it likely that (customised) readers will be able to parse it?

To put it another way, if I conform to the spec, but using JSON instead of XML is it a usable RSS feed?

edit I am not sure I made myself clear. There is no XML involved. I want to write something akin to RSS (which is XML) using JSON instead. Obviously the readers of such a feed would need to be written to understand the JSON format. I was wondering if this had been done already. Are there services that serve a feed this way? Are there programs that can aggregate/understand this format. Is the RSS spec (sans the XML part) a useful spec to conform to in this case?

rg

{
"title":"example.com",
"link":"http://www.example.com/",
"description":"Awesome news about junk",
"items":[
    {
        "title":"An article",
        "link":"http://www.example.com/an-article",
        "descrition":"Some sample text here",
        "pubDate":"2008-10-27 11:06 EST",
        "author":"example author",
    },
    {
        "title":"Second",
        "link":"http://www.example.com/Seond",
        "descrition":"Some sample text here",
        "pubDate":"2008-10-25 23:20 EST",
        "author":"author mcauthor",
    },
    {
        "title":"third article",
        "link":"http://www.example.com/third-article",
        "descrition":"Some sample text here",
        "pubDate":"2008-10-25 23:18 EST",
        "author":"some other author",
    }
]
}
+12  A: 

No, RSS is an XML-based format, and JSON is an different language rather than some kind of dialect. RSS readers won't understand JSON.

Your question is akin to asking 'Can I speak French in Chinese?'

Gareth
So there is not agreed upon standard for JSON to act as an RSS feed (not actual RSS but something that works on the same idea)? Seems like it would be a good thing to have. The RSS2.0 one could be a good start.
Hyposaurus
+1  A: 

There are a bunch of different ways to serialize RSS into JSON. All of them work pretty much the same way: the elements and attributes become property names, the values become property values, etc. See Piyush Shah's technique, for example, which is a .NET implementation.

Transforming arbitrary XML to JSON using XSLT is simple enough that you can find a half-dozen examples of it on Google.

As long as this is done consistently, JavaScript that can process an object model designed to replicate the data structure of the RSS specification should be able to handle the object model that the JSON deserializes into.

Who are you planning to send this JSON to? That's the real question.

Robert Rossney
+4  A: 

I believe this has been done already.

Take a look at this jQuery extension: jFeed - RSS/ATOM feed parser

jQuery.getFeed(options);

Options:

  • url:
  • data:
  • success:

Example:

jQuery.getFeed({
       url: 'rss.xml',
       success: function(feed) {
           alert(feed.title);
       }
   });

Note that in this case, 'feed' would be a javascript object. If you wanted to pass this using JSON, you can just use the javascript JSON utility.

Example:

var myJSONText = JSON.stringify(feed);
Dan Esparza
+2  A: 

Json.NET - http://james.newtonking.com/projects/json-net.aspx - has support to convert any XML document to JSON.

XmlDocument doc = new XmlDocument();

doc.LoadXml(@"<?xml version=""1.0"" standalone=""no""?>
<root>
  <person id=""1"">
    <name>Alan</name>
    <url>http://www.google.com&lt;/url&gt;
  </person>
  <person id=""2"">
    <name>Louis</name>
    <url>http://www.yahoo.com&lt;/url&gt;
  </person>
</root>");


string jsonText = JavaScriptConvert.SerializeXmlNode(doc);
//{
//  "?xml": {
//    "@version": "1.0",
//    "@standalone": "no"
//  },
//  "root": {
//    "person": [
//      {
//        "@id": "1",
//        "name": "Alan",
//        "url": "http://www.google.com"
//      },
//      {
//        "@id": "2",
//        "name": "Louis",
//        "url": "http://www.yahoo.com"
//      }
//    ]
//  }
//}

XmlDocument newDoc = (XmlDocument)JavaScriptConvert.DeerializeXmlNode(jsonText);

Assert.AreEqual(doc.InnerXml, newDoc.InnerXml);
James Newton-King
+4  A: 

Is the RSS spec (sans the XML part) a useful spec to conform to in this case?

If you want to invent yet another syndication format, I recommend using Atom as a base. IMHO it has much cleaner, more consistent design, and has useful features like reliable updates of past items, makes distinction between summaries and full content, etc.

I was wondering if this had been done already.

Flickr has JSON output format. They even have lolcode feed.

porneL
Voted up just for the link to the lolcode, which made me giggle most unprofessionally.
TRiG
+1  A: 

Well, if you are developing some javascript app you might want to get any RSS feeds as JSON to overcome cross-domain querying issue. There is a reliable Google provided solution that converts about any RSS to JSON. For jQuery lover's there is a universal RSS to JSON converter plugin.

Example:

$.jGFeed('http://twitter.com/statuses/user_timeline/26767000.rss',
  function(feeds){

    // feeds is a javascript object with RSS content

  }, 10);
Uzbekjon