As part of my homework I have to make a JSON file with class objects in it. I could do it, but the result is not that i expected.
import json
stList = []
class Student(object):
neptun_code = ""
result = 0
mark = 0
def make_student(neptun_code, result, mark):
student = Student()
student.neptun_code = neptun_code
student.result = result
student.mark = mark
stList.append(student)
def create_json():
json_string = json.dumps([ob.__dict__ for ob in stList])
with open("./data/student.txt", "w") as file:
json.dump(json_string, file)
Sample inputs for make_student
: test_code, test_result, test_mark
Here is the output in student.txt:
"[{\"neptun_code\": \"test_code\", \"result\": \"test_result\", \"mark\": \"test_mark\"}]"
there are plenty of unwanted characters in it. I would like to have something like this:
[{"neptun_code": "test_code", "result": "test_result", "mark": "test_mark"}]
Can anyone explain how to do this?