Presenting a more informative error message than “No JSON object could be decoded.”

I’ve discovered that the simplejson module provides more detailed error messages compared to the built-in json module, especially in scenarios like having a comma after the last item in a list:

json.loads('[1,2,]')
....
ValueError: No JSON object could be decoded

which is not very descriptive. The same operation with simplejson:

simplejson.loads('[1,2,]')
...
simplejson.decoder.JSONDecodeError: Expecting object: line 1 column 5 (char 5)

Much better! Likewise for other common errors like capitalizing True.

Published by Nhat Truong

Hi

Leave a comment