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