Serializing a complex data structure

We can also create JSON documents from Python data structures. Because Python is extremely sophisticated and flexible, we can easily create Python data structures that cannot possibly be represented in JSON.

The serialization to JSON works out the best if we create Python objects that are limited to simple dict, list, str, int, float, bool, and None values. If we're careful, we can build objects that serialize rapidly and can be used widely by a number of programs written in different languages.

None of these types of values involve Python sets, or other class definitions. This means that we're often forced to convert complex Python objects into dictionaries to represent them in a JSON document.

As an ...

Get Modern Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.