0

I am trying to dump this json -

{'total_run_count': 9, 'task': 'tasks.add', 'enabled': True, 'schedule': {'period': 'seconds', 'every': 3}, 'kwargs': {'max_targets': 100}, 'running': False, 'options': {}, 'delete_key': 'deleted:tasks:meta:newtask', 'name': b'tasks:meta:newtask', 'last_run_at': datetime.datetime(2016, 10, 3, 19, 9, 50, 162098), 'args': ['3', '2'], 'key': 'tasks:meta:newtask'}

and it fails for the key 'name'. I have decoded it in utf-8 but still not luck. I am getting the following error.

TypeError: 'tasks:meta:newtask' is not JSON serializable

what is not serializable about the above string ? I am clueless.

Shamik
  • 1,591
  • 2
  • 16
  • 36
  • 1
    try json.dumps(yourObject.__dict__), check answers here http://stackoverflow.com/questions/10252010/serializing-python-object-instance-to-json – Haifeng Zhang Oct 03 '16 at 19:17
  • "Still not luck" -- meaning what, *exactly*? When I try to reproduce this, and have decoded the bytestring, what I then have is the following: `TypeError: datetime.datetime(2016, 10, 3, 19, 9, 50, 162098) is not JSON serializable` -- which it's indeed not. But that's a different error, and your question should reflect that. – Charles Duffy Oct 03 '16 at 19:33
  • @Shamik, ...show us *how* you're trying to decode the object (and then serialize the result), so we can tell that it's done correctly. – Charles Duffy Oct 03 '16 at 19:35
  • I have a custom decoder inside which i am handling for datetime objects. But somehow even after decoding the byte string i am getting the above error. The error doesn't have the byte, check . – Shamik Oct 03 '16 at 19:36
  • @Shamik, *show us*: Provide a reproducer people who aren't you can run that generates your errors even after your attempts to avoid it. Right now, you're *saying* that you fixed some relevant issues, but without seeing the details of the implementation, we have no possible way of finding still-outstanding problems. See also http://stackoverflow.com/help/mcve – Charles Duffy Oct 03 '16 at 20:40

2 Answers2

1

Notice how that item is displayed in the dictionary:

'name': b'tasks:meta:newtask'

That leading b indicates that 'tasks:meta:newtask' is a byte string, not a regular character string. JSON is telling you that it doesn't know how to handle a byte string object.

Does it really need to be a byte string? If not, you should convert it to a regular string before calling json dump.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • I have handled it by decoding it in utf-8. If you see the error it doesn't have the leading b. – Shamik Oct 03 '16 at 19:23
  • _I have handled it_ So you've solved your problem then? Great! – John Gordon Oct 03 '16 at 19:28
  • @Shamik, if what you're actually trying to JSON-serialize differs from what's given in your question (such as because you're modifying the bytestring to be a character string instead), *update the question to reflect the actual data* -- but make sure (completely, 100% sure) that it can be reproduced in that state. – Charles Duffy Oct 03 '16 at 19:30
1

The "name" value in your dict is a bytes object, not string. You have to decode it or you can write your custom JSON encoder:

import json

def default(o):
    if isinstance(o, bytes):
        return o.decode()
    return json.JSONEncoder.default(self, o)

data = {'name': b'tasks:meta:newtask'}
json.JSONEncoder(default=default).encode(data)
skovorodkin
  • 9,394
  • 1
  • 39
  • 30