-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.

6
  • If you need more information feel free to ask me here Commented Feb 23, 2021 at 17:01
  • Badly indented python code is invalid python code!. Please fix your indentation. Also, please post a minimal reproducible example 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 minimal reproducible example so that the people helping you can run that code easily Commented Feb 23, 2021 at 17:12
  • Does this answer your question? How to erase the file contents of text file in Python? Commented Feb 23, 2021 at 17:14
  • @PranavHosangadi gee, thanks Commented Feb 23, 2021 at 17:16
  • the linked duplicate says exactly what your accepted answer does. Why the sarcasm? Commented Feb 23, 2021 at 18:44

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that helped. How would I fix this?
I added it to the answer.
thanks! ill look at the docs and accept this answer once i test it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.