I know there are quite a few json/python questions out there but I can't seem to figure this one out. I am trying to serialize two lists into the same file. In order to do that I create a new class that holds the two lists:
class newJSON(object):
def __init__(self, list1, list2):
self.data = {'data': list1, 'info' : list2}
I need the resulting data file to look like the following:
{
"data" : [
{
"name" : "aName" ,
"coordinates" : {"obj2" : 33, "obj3" : 71}
} , {
"name" : "bName" ,
"coordinates" : {"obj2" : 12, "obj3" : 77}
}
] ,
"info" : [
{
"first" : ["xxx" , "yyy"] ,
"space" : 21
} , {
"first" : ["aaa" , "bbb"] ,
"space" : 12
}
]
}
So then I go to decode the object as recommended in Serializing python object instance to JSON and several others:
jsonToEncode = newJSON(myList1, myList2)
myNewJSONData = json.dumps(jsonToEncode.__dict__)
However I get the "is not JSON serializable error"... I have tried this with and without the dict but to no success. The JSON must be in the format shown above. What is the problem?
Thanks
****EDIT****
in order to make the two lists, I take a json file which is formatted exactly like the json shown and do the following:
list1 = [obj1(**myObj) for myObj in data["data"]]
and the same for list2. obj1 is made like this:
class obj1(object):
def__init__(self, name, coordinates):
self.name = name
self.coordinate = coordinates