Python JSON can't save and open the file

17 hours ago 3
ARTICLE AD BOX

When I try to save or open a JSON file, I get the error json.decoder.JSONDecodeError: Expecting value: line 1 column 21 (char 20)
And it appears only if I try to save variables to a file, but if I save stock values, for example:

'current_user_id': 1, 'is_logged_in': True

Then everything works. But it's the variables that I need to save. I would be grateful for your help. Here is the code:

self.current_user_id = None self.is_logged_in = False self.load_state() def save_state(self): with open('user_state.json', 'w') as f: json.dump({ 'current_user_id': self.current_user_id, 'is_logged_in': self.is_logged_in }, f) def load_state(self): if os.path.exists('user_state.json'): with open('user_state.json', 'r') as f: data = json.load(f) self.current_user_id = data.get('current_user_id') self.is_logged_in = data.get('is_logged_in', False) else: self.current_user_id = None self.is_logged_in = False
Read Entire Article