-1

when I remove an object from an array in json with python, it ruins my json file. For example here is my python:

srs = server["purchaseableRoles"]
                        if srs == []:
                            await ctx.send("Your server has no purchaseable roles at the moment, you cant remove one.")
                        else:
                            for i in srs:
                                splitsrs = i.split("|")
                                if int(splitsrs[0]) == role.id:
                                    print(str(i))
                                    srs.remove(str(i))
                                    print(srs)
                                    f.seek(0)
                                    json.dump(serversjson,f,sort_keys=True,indent=4)
                                    await ctx.send("removed the role from the role shop!")

(please ignore the discord.py stuff, its in a discord bot. Also ignore the weird indents I don't know why it pasted in like that) That code is pretty much removing an object from an array in a json file, and updating it. Except for when I do that, here is my json:

{
    "servers": [
        {
            "id": 1234,
            "purchaseableRoles": [
                "1234|5678"
            ]
        },
        {
            "id": 813070255127396352,
            "purchaseableRoles": []
        }
    ]
}071595911774238|5"
            ]
        }
    ]
}

it just gets all messed up. Here was my json before the mess-up:

{
    "servers": [
        {
            "id": 1234,
            "purchaseableRoles": [
                "1234|5678"
            ]
        },
        {
            "id": 813070255127396352,
            "purchaseableRoles": [
                "813071595911774238|5"
            ]
        }
    ]
}

is there a way to prevent this? Thanks in advance if you help.

HoneyPoop
  • 473
  • 1
  • 6
  • 25
  • If you need more information feel free to ask me here – HoneyPoop Feb 23 '21 at 17:01
  • Badly indented python code is _invalid_ python code!. Please fix your indentation. Also, please post a [mre] that people can run to reproduce your problem. Simply pasting your code here isn't acceptable. You need to put in some effort to convert it to a [mre] so that the people helping you can run that code easily – Pranav Hosangadi Feb 23 '21 at 17:12
  • Does this answer your question? [How to erase the file contents of text file in Python?](https://stackoverflow.com/questions/2769061/how-to-erase-the-file-contents-of-text-file-in-python) – Pranav Hosangadi Feb 23 '21 at 17:14
  • @PranavHosangadi gee, thanks – HoneyPoop Feb 23 '21 at 17:16
  • the linked duplicate says exactly what your accepted answer does. Why the sarcasm? – Pranav Hosangadi Feb 23 '21 at 18:44
  • sorry, at the time I didn't know that I needed to erase the contents of the file to fix the error – HoneyPoop Feb 24 '21 at 05:54

1 Answers1

2

You are moving the file cursor to the begining of the file and overwrite the Bytes in the file with Bytes in the JSON string.

But you removed some stuff, so the string that overwrites the file is shorter than the string in the file.

So at the end of the file, some of the old string remains.

There is a method file_obj.truncate(). But there is little documentation on it in Python3, apparently. Something like this:

f.seek(0)
f.truncate()
C14L
  • 12,153
  • 4
  • 39
  • 52