I am having some difficulties building JSON files out of fairly complex python objects.
The class which I am trying to build into a JSON file looks like this:
class Recipe:
def __ini__(self):
self.recipeTitle = ""
self.recipeDiscription = ""
self.recipeMetaData = []
self.reipeIngredients = collections.OrderedDict()
self.instructions = []
self.searchableIngredients = []
self.allIng = []
self.tags = []
self.ingredientMetadata = []
# self.getAllIngredients()
I tried building a dictionary where the keys were the string name of the attribute, and the values were the contents of the attribute. However, the json.dump() didn't like the dictionary of lists, dicts, and strings. Do I manually have to create the logic for populating the JSON file, such as creating a string by joining all the contents of a list and delimiting it in a unique way, or is there an easier way to translate such an object into JSON?
For reference, a populated Recipe object would look something like this:
recipe.recipeTitle = "Pancakes"
recipe.recipeDiscription = "Something really tasty"
recipe.metaData = ["15 Minutes", "Serves 5"]
recipe.recipeIngredients = {('For the sauce', ['sugar', 'spice',
'everything nice']),('For the food', ['meat', 'fish', 'eggs'])}
recipe.instructions = ['First, stir really well', 'Second - fry']
self.allIng = ['sugar', 'spice', 'everything nice', 'meat', 'fish',
'eggs']
self.tags = [1252, 2352, 2174, 454]
self.ingredientMetadata = [1,17,23,55,153,352]
I'm stuck trying to turn this into JSON, and any help would be greatly appreciated!
Thank's in advance.