0

I want to completely empty a JSON file, while still keeping the file. How do I do that? I tried:

with open("Screentime/activities.json", "w") as f:
    json.dump("", f, indent=4, sort_keys=True)
with open("Screentime/activities.json", "w") as f:
    json.dump({}, f, indent=4, sort_keys=True)

, etc but none of them work. Please help me out! Ive seen answers on how to delete keys in the JSON, or delete the entire file, but not how to just replace it with nothing.

Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50
Leon_K
  • 117
  • 1
  • 10
  • How do you know that `none of them work`? – quamrana Oct 30 '20 at 16:39
  • There is no such thing as an "empty JSON file" – there is just an empty file. Once you write JSON to it, it is no longer empty! – MisterMiyagi Oct 30 '20 at 16:41
  • Are you looking to have a zero length file or your existing JSON with empty values? – PM 77-1 Oct 30 '20 at 16:41
  • Well, I mean that none of the ones that I tried have worked. Those methods were just replacing the object with "", {}, "{}", etc. They all didnt do anything or they wrote something like {} – Leon_K Oct 30 '20 at 16:42
  • Yes, but what makes you say they didn't work? Why is a file with `"{}"` in it `not working`? – quamrana Oct 30 '20 at 16:43
  • Im sorry, I meant I want to clear all the existing JSON code on that file so that I am left with an empty json file after clearing it. Riccardo Bucco's method works. Didnt know that this was a thing. – Leon_K Oct 30 '20 at 16:45
  • An empty `json` file is either `{}` or `[]`. Anything else is not `json`. – quamrana Oct 30 '20 at 16:46
  • 1
    @quamrana Not entirely true. A single string, number or boolean value are also considered a valid json. Here is an online validator https://jsonlint.com/. Here are some great SO answers https://stackoverflow.com/questions/18419428/what-is-the-minimum-valid-json. If you try to read a file with an empty string "" or a number with python `json` module no exceptions are raised. So at least the python implementation does accept something different to `{}` and `[]`. Still my answer clearly does not create a valid JSON :) The OP was not very clear about it. – Riccardo Bucco Oct 30 '20 at 16:55
  • I think you mean that sensible implementations of `json` allow these things. If you go to `json.org`, then `JSON is built on two structures`, which are object `{}` or array `[]`. (btw I up-voted your answer since it solves the problem). – quamrana Oct 30 '20 at 16:59
  • 1
    @quamrana No I'm referring to the ECMA-404 standard. As you can read in the specifications "A JSON value can be an object, array, number, string, true, false, or null." (don't worry, I'm not interested in votes, just curious because before writing this answer i did not know anything about this cornercase in valid json, I'm just reading something now thanks to your useful comment :) ) – Riccardo Bucco Oct 30 '20 at 17:08

1 Answers1

2

You can just open and close the file in 'w' mode:

open('Screentime/activities.json', 'w').close()

This is going to remove completely any content from your file. In other words, your file will be empty.

EDIT: While this answer does provide the result the OP asked for, it should be noted that an empty file is not a valid JSON file. More details can be found here.

Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50