Run
import dataclasses import json from dataclasses import dataclass @dataclass class Person: name: str age: int @dataclass class Student(): student_id: str basic_details: Person # Create an instance of dataclass Person student_1 = Student("12345", Person("Ram", 21)) # Convert dataclass to dictionary student_1_dict = dataclasses.asdict(student_1) # Convert dictionary to JSON string student_1_json = json.dumps(student_1_dict) print(f"JSON String\n{student_1_json}")
Output