Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Which is faster to parse Json or XML? [closed]

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 ?).

like image 733
Aamir Rind Avatar asked Oct 19 '11 07:10

Aamir Rind


People also ask

Is JSON parsing faster than XML?

JSON is faster because it is designed specifically for data interchange. JSON encoding is terse, which requires less bytes for transit. JSON parsers are less complex, which requires less processing time and memory overhead. XML is slower, because it is designed for a lot more than just data interchange.

Is JSON parse fast?

parse is a slow way to create a copy of an object.

Why is JSON more lightweight than XML?

(a) the JSON data model is simpler; it has fewer different kinds of object and they have fewer properties. (c) the serialized syntax of JSON has less redundancy (is less verbose) than the syntax of XML. Of course, these differences are because JSON was designed for a different purpose than XML.


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.

like image 131
Ferdinand Beyer Avatar answered Sep 23 '22 23:09

Ferdinand Beyer