I created an object looks like this:
class Rectangle:
def __init__(self, ymin, xmin, ymax, xmax):
self.y_min = ymin
self.x_min = xmin
self.y_max = ymax
self.x_max = xmax
def tojson(self):
return json.dumps(self, default=lambda o: o.__dict__,
sort_keys=False, indent=4)
I have a more complex object but it is enought to explain. If a create a new Rectangle and call tojson() I get the object in the format I need.
rect1 = Rect(1,2,3,4)
print(rect.tojson())
result:
{
"y_min": 1,
"x_min": 2,
"y_max": 3,
"x_max": 4
}
But if I have Rectangle list and I want to create a JSON, I get them in one row.
rects = [rect1, rect2]
print(json.dumps(rects, default = lambda x: x.__dict__))
result:
[{"y_min": 1, "x_min": 2, "y_max": 3, "x_max": 4}, {"y_min": 1, "x_min": 2, "y_max": 3, "x_max": 4}]
Could you please help me, how could I do this properly? Thank you in advance for your help!

print(json.dumps(rects, default = lambda x: x.__dict__), indent=4)