0

I have a dictionary

data_msg = {'id1': [{'id2': ['message_obj', 'message_obj1']}, {'id3': ['message_obj', 'message_obj1']}], '247217277613768704': [{'247217277613768704': [<Message id=747569642603806861 channel=<TextChannel id=745834933808791603 name='verification' position=5 nsfw=False news=False category_id=619309021316513794> type=<MessageType.default: 0> author=<Member id=745497933662650369 name='Rep' discriminator='5005' bot=True nick=None guild=<Guild id=619309021316513792 name='Strictly Coaching' shard_id=None chunked=True member_count=10>> flags=<MessageFlags value=0>>]}]}

My code is as follows:

pend = 'bin/pending'
v_repped = 'bin/verified_repped'
message_bin = 'bin/message_bin'

def on_instance(data_instanced):
    if data_instanced == data:
        data_main = json.dumps(data_instanced)
        with open(pend, 'w') as instance:
            json.dump(data_main, instance)
    elif data_instanced == data_verified:
        data_v = json.dumps(data_instanced)
        with open(v_repped) as instance:
            json.dump(data_v, instance)
    elif data_instanced == data_msg:
        data_m = json.dumps(data_instanced)
        with open(message_bin) as instance:
            json.dump(data_m, instance)

Where data_msg, data_verified, and data are global variables to store dictionaries. I went ahead and gave the data for "data_msg" as that seems to be the one that is not working.

The error I get is as follows: TypeError: Object of type Message is not JSON serializable

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • You cannot save a `Message` object as JSON. There is no such data type. If you plan to open the file in another Python program, use `pickle`. – DYZ Aug 24 '20 at 21:49
  • How could I convert this to pickle? I know nothing about that. I would really appreciate it. Also this is the worst way to store data, I know, SQL is obviously better, but im making this program for free so it's alright. – Brendan Nnsp Jorgenson Aug 24 '20 at 21:58
  • Pickle is well documented. Just read about it. – DYZ Aug 24 '20 at 22:14
  • Does this answer your question? [How to make a class JSON serializable](https://stackoverflow.com/questions/3768895/how-to-make-a-class-json-serializable) – Pranav Hosangadi Aug 25 '20 at 18:16

0 Answers0