0

I am using Python 2.6 with simplejson version 3.10.0

I am new to Python so please forgive any errors in code or lack of "python-ness". I am open to constructive criticism of course.

I have a custom object I created with three attributes (min, max, and value). I am adding a list of these objects as a value to a key in a dictionary. The key is a variable name and the value is the list of custom objects. I want to do a JSON dump of this dictionary and have something that looks like this:

{
"transforms": {
    "variable1":[
      {min:200,max:300,value:10},
      {min:300,max:500,value:20}
    ]
  }
}

The problem is I keep getting this error when doing a json dumps call:

TypeError: {min:200,max:300,value:10} is not JSON serializable

There is probably something I am missing or doing wrong with defining my custom object, but I haven't been able to figure it out. Here is the full Python code:

def createTransform(minBin, maxBin, value):
    transform = Transform(minBin, maxBin, value)
    return transform

def createDict():
    finalDict = {}
    varTransforms = []
    varTransforms.append(createTransform(200, 300, 10))
    varTransforms.append(createTransform(400, 500, 20))
    finalDict.update({"variable1":varTransforms})
    return finalDict


class Transform:
    minBin = 0
    maxBin = 0
    value = 0

    def __init__(self, minBin, maxBin, value):
        self.minBin = minBin
        self.maxBin = maxBin
        self.value = value

    def __str__(self):
        return "{" + "min:" + str(self.minBin) + "," + "max:" + str(self.maxBin) + "," + "value:" + str(self.value) + "}"

    def __repr__(self):
        return self.__str__()

allTransforms = createDict()
print json.dumps({'transforms': allTransforms})

Any help is appreciated it, thank you.

Also, I am able to get this to work if I change allTransforms = createDict() to allTransforms = str(createDict())

but it results in having extra quotations and doing a .strip('"') on the result of Json.dumps does not remove them.

Seephor
  • 1,692
  • 3
  • 28
  • 50
  • Custom objects are not JSON serialisable, no. The `__repr__` output does make this more confusing, but it's *not* a dictionary. – Martijn Pieters Dec 29 '16 at 17:07
  • @MartijnPieters so do I need to change the list of transforms into a dictionary of transforms? and have that be the value to the key? – Seephor Dec 29 '16 at 17:09
  • @MartijnPieters I tried using the answer in the question marked as duplicated, but calling .__dict__ on my allTransforms variable resulted in this error: AttributeError: 'dict' object has no attribute '__dict__' – Seephor Dec 29 '16 at 17:13
  • You are calling that on the wrong object. Instead of using `varTransforms.append(createTransform(200, 300, 10))`, use `varTransforms.append(vars(createTransform(200, 300, 10)))` (`vars()` returns the `__dict__` object, it's a clearer way of spelling what you want). – Martijn Pieters Dec 29 '16 at 17:15
  • @MartijnPieters got it. thank you that worked. – Seephor Dec 29 '16 at 17:28

0 Answers0