from copy import deepcopy from pathlib import Path from shutil import copy import json journal_path = Path.home() / '.journal.json' copy(str(journal_path), str(journal_path.with_suffix('.bkp'))) journal = json.loads(journal_path.read_text()) new_journal = deepcopy(journal) for day in journal['days']: new_entries = [] for entry in journal['days'][day]['entries']: new_blocks = [] for block in entry['blocks']: if not isinstance(block, str) and block['type'] == 'hide': if len(new_blocks) and not isinstance(new_blocks[-1], str) and \ new_blocks[-1]['type'] != 'tag': new_blocks.append({'type': 'tag', 'value': ['hide']}) elif not isinstance(block, str) and block['type'] == 'info': new_blocks.append({'type': 'tag', 'value': ['info']}) new_blocks.append('\n') new_blocks.append(block['value']) else: new_blocks.append(block) entry['blocks'] = new_blocks new_entries.append(entry) new_journal['days'][day]['entries'] = new_entries journal_path.write_text(json.dumps(new_journal))