Nice to meet you, ma fren 🫡. Sorry, I ain't DEVing that much ✍️ , primarily due to the nature of maintaining Open Source projects 👷, while also gigging 💰. Anyways, stay humble like a bumblebee 🐝.
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.
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.
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:
By coincidence I recently encountered the reason why item 7 "Pretty print a dict." is not reliable.
i.e. Answer: Using json.dumps method.
This will fail when the items in the dictionary are not things that can be expressed as JSON. The two I encountered were enumerations and tuples.
There is a library to solve that, check orjson, its faster, it parses more types, check it out
Actually, I don't like to use this method, but rather engineer my own thing. For instance, at some point in the past, i created these handy functions:
Usage:
It seems like, over the years, I developed a hate relationship with the DRY principle. Anyways, I hope you find it useful. Cheers mate!
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()
Check orjson, faster and better json handling
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.
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.
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: