Question

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",
    }
]
} 
Was it helpful?

Solution

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);

OTHER TIPS

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?'

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.

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</url>
  </person>
  <person id=""2"">
    <name>Louis</name>
    <url>http://www.yahoo.com</url>
  </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);

You're right that the client reading the feed would have to have custom support for whatever the particulars of your JSON were. So you'd either need to make a custom feed reader to consume that information, or someone would have to propose a JSON feed standard, and it'd have to be widely adopted.

Well, I think your desires have finally been met, friend!

Take a look at JSON Feed. As of this writing it's only about a week old, but it's already picking up steam, having been supported now by Feedly, Feedbin, News Explorer, NewsBlur, and more being added all the time.

If I had to pick a standard to use when generating a JSON version of RSS, I'd pick JSON Feed for sure.

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.

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);

I know this is a fairly old question, and maybe irrelevant now.

However. I would suggest anyone looking to publish a RSS-like feed in JSON should use a new format that is rapidly gaining adoption; JSONFeed (https://jsonfeed.org).

Are there programs that can aggregate/understand this format.

You can use XSLT 3.0 to transform XML to JSON and back to XML again. More info on how to accomplish the transforms to JSON here:

https://www.xml.com/articles/2017/02/14/why-you-should-be-using-xslt-30/.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top