Two functions analyze completely different languages - JSON and Python literal syntax. * Because literal_eval says:
A string or node can only contain the following literary Python structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None .
JSON , by contrast, only double-quoted string literals (not quite identical to Python **), JavaScript numbers (only int and float ***), objects (approximately equivalent to dicts), arrays (approximately equivalent to lists), JavaScript booleans (which are processed) are processed differ from Python) and null .
The fact that these two languages have some overlap does not mean that they are one and the same language.
Why is json.loads so much faster?
Since Python literal syntax is a more complex and powerful language than JSON, it will most likely be parsed more slowly. And, perhaps more importantly, because the literal syntax of Python is not intended to be used as a data exchange format (in fact, it is not specifically intended for this), no one can put much effort into speeding up data exchange. ****
This question seems to mean that ast is more flexible with respect to input (double or single quotes)
These are raw string literals, as well as Unicode and literal string literals, as well as complex numbers and sets, as well as all other things that JSON does not process.
Are there any cases where I prefer to use ast.literal_eval over json.loads, although it is slower?
Yes. When you want to parse Python literals, you should use ast.literal_eval . (Or, better yet, think about your design so you don't want to parse Python literals ...)
* This is a slightly vague term. For example, -2 not literal in Python, but an operator expression, but literal_eval can handle this. And, of course, tuple / list / dict / set mappings are not literals, but literal_eval can process them, except that messages are also displayed, and literal_eval cannot process them. Other functions in the ast module can help you find out what actually is and is not a literal - for example, ast.dump(ast.parse("expr")) .
** For example, "\q" is a JSON error.
*** Technically, JSON handles only one type of "number", which is a floating point. But the Python json module parses numbers without a decimal point or exponent as integers, and the same is true for many JSON modules in other languages.
**** If you missed Tim Peters' comment on the question: " ast.literal_eval so easy to use that no one felt it was worth the time to work (work, work), speeding it up. On the contrary, JSON libraries are usually used to analyze gigabytes data.