You should really have clarified you're using the simplejson library, not the default json module. I'm assuming you've got something like import simplejson as json
? That's a questionable decision if anyone else is going to look at this code, as no one is expecting json
to refer to simplejson
. (Nothing wrong with using simplejson, just don't import it as json.)
The error in your code wouldn't be thrown by the example you have at the top, e.g.:
>>> import simplejson as json
>>> json.dump({ 'id' : '3' })
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: dump() takes at least 2 arguments (1 given)
The error you're showing in your question is likely because you're trying to create a JSON data structure from an object which doesn't serialize. We'd need to see your object to see what's really failing and provide a better solution.
For example:
>>> class demoObj:
... def __init__(self):
... self.a = '1'
...
>>> testObj = demoObj()
>>> json.dumps(testObj)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 264, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python2.7/json/encoder.py", line 178, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <__main__.demoObj instance at 0xe5b7a0> is not JSON serializable
Note that the error here is the same as the error you have (except for the reference to the object).
To serialize my testObj, I basically need to be able to get a dictionary out of it. Some references for doing this can be seen in these questions: