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.
Answer from Ferdinand Beyer on Stack OverflowVideos
Can I convert data between XML and JSON?
Yes, data can be converted between XML and JSON using various tools and libraries available in most programming languages like xmltodict and dicttoxml in python and xml2js and js2xmlparser in node.js. There are also numerous online converters that can quickly transform XML data to JSON format and vice versa.
What are the primary differences between JSON and XML?
The primary differences between JSON and XML lie in their structure and syntax. JSON is a lightweight data format that uses key-value pairs, making it easy to read and write. In contrast, XML utilizes a more verbose syntax with tags to represent data, which can add complexity. While JSON is primarily used for data interchange in web APIs, XML excels in data representation requiring strict validation and better compatibility with legacy systems.
What is JSON?
xmltodict (full disclosure: I wrote it) can help you convert your XML to a dict+list+string structure, following this "standard". It is Expat-based, so it's very fast and doesn't need to load the whole XML tree in memory.
Once you have that data structure, you can serialize it to JSON:
import xmltodict, json
o = xmltodict.parse('<e> <a>text</a> <a>text</a> </e>')
json.dumps(o) # '{"e": {"a": ["text", "text"]}}'
There is no "one-to-one" mapping between XML and JSON, so converting one to the other necessarily requires some understanding of what you want to do with the results.
That being said, Python's standard library has several modules for parsing XML (including DOM, SAX, and ElementTree). As of Python 2.6, support for converting Python data structures to and from JSON is included in the json module.
So the infrastructure is there.
I keep seeing comparison posts where the only answers are "JSON is better because it's readable." And just looking up info about XML results in "JSON is better because it's readable and easy to send online."
My case is not for web services, but rather a single player game with a mechanic that needs tables that aren't human readable without UI. Is there any advantages to XML at all?