How can I preserve Python tuples with JSON?

PythonProgramming

There is no concept of a tuple in the JSON format. Python's JSON module converts Python tuples to JSON lists because that's the closest thing in JSON to a tuple. Immutability will not be preserved. If you want to preserve them, use a utility like a pickle or write your own encoders and decoders.

If you're using pickle, it won't store the Python temples in JSON files but in pkl files. This isn't useful if you're sending data across the web. The best way is to use your own encoders and decoders that will differentiate between lists and tuples depending on how you chose to make the encoders and decoders work.

raja
Published on 18-Apr-2018 12:44:50
Advertisements