How can I preserve Python tuples with JSON?


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.

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

184 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements