How can I represent python tuple in JSON format?


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 pickle or write your own encoders and decoders.

If you're using pickle, it won't store the Python tumples 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.

Published on 18-Apr-2018 10:40:00