0
json.dump({'id' : '3'})

  File "/Library/Python/2.7/site-packages/simplejson/__init__.py", line 354, in dumps
    return _default_encoder.encode(obj)
  File "/Library/Python/2.7/site-packages/simplejson/encoder.py", line 262, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Library/Python/2.7/site-packages/simplejson/encoder.py", line 340, in iterencode
    return _iterencode(o, 0)
  File "/Library/Python/2.7/site-packages/simplejson/encoder.py", line 239, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: ObjectId('520183b519744a0ca3d36003') is not JSON serializable

What is wrong?

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
Robin D.
  • 41
  • 1
  • 3
  • `dumps` istead of `dump`. Also is there any particular reason that you are using `simplejson` instead of the standard library `json` module? – Viktor Kerkez Aug 06 '13 at 23:42
  • Can you add the actual object you're trying to serialize so we can see why it's throwing the error you've shown? The example you've given isn't the same as what's causing that error . . . – ernie Aug 06 '13 at 23:56

2 Answers2

2

Try using the standard json library and dumps instead.

With that change it works fine for me.

>>> import json
>>> json.dumps({'id' : '3'})
'{"id": "3"}'
Matt Bryant
  • 4,841
  • 4
  • 31
  • 46
0

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:

Community
  • 1
  • 1
ernie
  • 6,356
  • 23
  • 28
  • Maybe because it's common practice to `import somename as name`. Example: `from xml.etree import ElementTree as etree` and `from lxml import etree`. This is so that the user can swap between them without having to change every usage. – tshepang Aug 07 '13 at 05:59