From the course: Effective Serialization with Python
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Solution: Convert log records to JSON - Python Tutorial
From the course: Effective Serialization with Python
Solution: Convert log records to JSON
(upbeat electronic music) - [Instructor] The problem is two types, the datetime and the IP address, which are not serialized by default in JSON. So we create our own default function that gets an object. If this object is a datetime, we convert it to a string using the isoformat method. If it's an IP address, we use the str to convert it to a string. Then we iterate over the logs, convert them to JSON with calling asdict, which converts the log objects to a dictionary, and then using default with our own default function. And finally, we're going to print the data, which will print it to standard output. So python log.py. And we see our objects printed to standard outputs.