How can I serialize an object that contains a list of objects to JSON
The error I get :
TypeError: Object of type person is not JSON serializable
import json
class person:
def __init__(self, name, cars):
self._name = name
self._cars = cars
class car:
def __init__(self, brand, color):
self._brand = brand
self._color = color
Jason = person("Jason", [car("Toyota", "Blue"), car("Honda", "Red")])
json = json.dumps(Jason)
print(json)
The output I want :
{
"name": "Jason",
"cars":[
{
"brand": "Toyota",
"color": "Blue"
},
{
"brand": "Honda",
"color": "Red"
}
]
}