3

When should we use xml in REST services and when JSON?

What are pros and cons of using json instead of XML?

1
  • Web services returns the results as JavaScript object notation and XML, because these are the languages can be used to traverse and for Transportation around various devices. These languages are platform independent. Commented Oct 29, 2012 at 13:46

3 Answers 3

5

This is like asking:

"what are the pros and cons of speaking french vs speaking spanish".

It depends on the context and audience for whatever you are saying.

What are the needs of your service consumers? Are you providing results to other servers or to ajax resquests from browsers? Do you have a simple object, array, or a very complex object with possibly other objects inside the main one? By providing details like that you can list pros and cons. If you can only do one, this question will drive the choice.

I typically allow the client to specify the format they want and return either json or xml. It's just 2 different 'views' or representations of the same resource.

Sign up to request clarification or add additional context in comments.

2 Comments

I provide results to other servers, have a quite simple objects.
@AlexanderBezrodniy I'd allow for both formats (maybe put a get parameter ?type=json or ?type=xml. Create a simple conversion library to process your output based on the parameter. If only one choice, in this case json is usually easier to consume and troubleshoot.
3

I really cant tell what the pros and the cons are, both of those MIME types require serialization and deserialization of your data. What I can tell is that it's a good habit to offer to client applications the chance to request a specific content type so your service can be consumed by a variety of clients written in different languages.

Comments

2

I would agree with Ray, regarding how you should respond to clients (xml or json). Indeed is a good idea to implement both and give the client the choice to decide the format of the response. Most of the web services i have used support that and is fairly easy to do as you only need to implement two different deserialization/serialization methods for the same object.

I would say that lately more and more WS provide data in json format and it seems to me ore flexible and faster when serializing/deserializing. In addition i find JSON more human-readable especially in complex objects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.