DEV Community

Discussion on: 30 PyTricks I've Learned By Joining the Real Python Mailing List.

Collapse
 
tompollard61 profile image
Tom Pollard

json.dumps() will represent tuples as JSON lists. But there are certainly other Python types that json.dumps() can't handle, like datetimes

The pprint ("pretty-print") module handles all of those. So, instead of json.dumps() you can use pprint.pprint()

Collapse
 
gdledsan profile image
Edmundo Sanchez

Check orjson, faster and better json handling

Collapse
 
geraldew profile image
geraldew

Well it simply didn't in the environment that I was using, instead it crashed on the tuple. To be clear, the tuple was being used as a key in the Python dictionary, something that Python allows but I'm guessing has no equivalence in JSON.

Thread Thread
 
tompollard61 profile image
Tom Pollard

Yes, JSON keys need to be strings.

In any case, pprint was introduced to the stdlib specifically to handle pretty-printing of Python values, but I don't run across many people who seem to know about it.

Thread Thread
 
geraldew profile image
geraldew

post script - as it happened I was curious enough about JSON versus data structures to try getting it to handle something more than just text keys. So I wrote an experiment about using enumerations - see: