ProAnswers.org

why do people want to return both(JSON & XML) from the same service?

I understand why “REST framework” vendors want to provide the support for returning both Json based representations and XML based representations, but why do people want to return both from the same service?

1 Like

Json is often suitable for client side scripts. It is a super-lightweight response and the most part of JavaScript frameworks come with a parser built-in. On the other side, many server side applications and languages still rely heavily on XML. Just to name one: Java.

Of course, XML can be parsed with JavaScript and Java (and the most part of other programming languages) has at least one Json parser. But at the moment this seems to be the most common practice.

Talking about the “implementation vs benefit” topic, I mostly work with Ruby and I can tell you Ruby on Rails provides the ability to create a Json or XML response in less than a couple of seconds from the same source. In this case it’s not a problem and I usually add that feature if I think it could be useful.

With other technologies, for example PHP, it would require more effort and the implementation would have a different cost. Unless this would be a fundamental feature, I would probably stick with one response until I don’t find the need to provide to different versions.

There are 2 reasons I can see for returning both XML and JSON from the same service, the first is to ensure the service is only written once I don’t want to have to write and maintain two web services that do the same call, but one will return JSON and one will return XML data. Secondly it allows the caller to choose the format without having to specify two different URL’s, after all the code is the same it is only the data format is different.

Thinking about news feed formats on websites, they all tend to use the extension to determine the format so if I pass in .html I expect to receive HTML formatted data, if I pass in .rss I expect it to be formatted to RSS standards though the actual code in the back end is probably the same, just using a different template to format the data.

Thanks !!!