From my observations overall JSON
is faster to Parse
than XML
. I have found two good question regarding this. One is asked for PHP and other is asked for JavaScript. I want to know about python, how python is efficient with them? and which is more efficient to parse.
Also please help in choosing the best Python parser for XML (e.g. xmlparser library , lxml or ?) and JSON (simplejson, jsonlib or ?).

- 1
- 1

- 38,793
- 23
- 126
- 164
-
3Parsing time is typically not a big concern, with either format. Human readability, total size of the data (total # of bytes: JSON is typically smaller), amount of information stored (XML probably self-describes itself better), and ease of use (one or the other may be easier to deal with in code) all tend to outweigh parsing time. – Thanatos Oct 19 '11 at 07:56
-
1This blog says it is JSON http://codersbuffet.blogspot.com/2010/03/json-vs-xml-and-python-parsing.html – Narendra Yadala Oct 19 '11 at 07:57
-
@Thanatos and what about choice of parser? Basically i am working on an API which gives both xml and json response. I am thinking of lxml for xml prsing and for json parsing using simplejson would be better? The data is complex nested. – Aamir Rind Oct 19 '11 at 08:22
1 Answers
In my opinion, it does not make sense to compare XML and JSON parsing times. Choosing one format over the other depends on your use case.
If you only want to store primitive types as supported by JSON in a simple, human-readable format, JSON is the way to go. If you need all the power and complexity of a markup language, use XML. You probably don't want to invent a document format based on JSON.
The bottleneck with parsing JSON and XML usually is not the parsing itself, but the interpretation/representation of the data. An event-based XML parser usually is very fast, but building a complex DOM tree of thousands of small objects is not. If you need to parse XML to nested native data structures such as lists and dictionaries, the slow part will be the interpretation of the parsing results, not the actual string analysis. Since JSON parses right to those primitive types rather than a complex object tree, it will likely be faster.

- 64,979
- 15
- 154
- 145
-
There are situations where both JSON and XML are suitable for the purpose and speed of processing and memory requirements are important. Especially when there are too many concurrent requests (in a web server). – Mohammed Shareef C Feb 19 '23 at 08:25