I have a requirement where I'd like to construct a lot of JSON objects. And there are many different definitions and ideally I want to manage them like classes and construct objects and dump them to JSON on demand.
Is there an existing package/recipe that let's me do the following
For keeping it simple lets say I need to represent people who are working, studying or both:
[{
"Name": "Foo",
"JobInfo": {
"JobTitle": "Sr Manager",
"Salary": 4455
},
{
"Name": "Bar",
"CourseInfo": {
"CourseTitle": "Intro 101",
"Units": 3
}]
I'd like to create objects that can dump valid JSON, but created like regular python classes.
I'd like to define my classes like a db model:
class Person:
Name = String()
JobInfo = Job()
CourseInfo = Course()
class Job:
JobTitle = String()
Salary = Integer()
class Course:
CourseTitle = String()
Units = Integer()
persons = [Person("Foo", Job("Sr Manager", 4455)), Person("Bar", Course("Intro 101", 3))]
person_list = list(persons)
print person_list.to_json() # this should print the JSON example from above
EDIT
I wrote my own mini-framework to accomplish this. Its available via pip
pip install pymodjson
Code and examples are available here: (MIT) https://github.com/saravanareddy/pymodjson