# Buggy code
def clean_json(path, target):
    """Delete key in the JSON file"""
    import json

    changed = False
    targets = target.split("/")

    # read file to parser
    with open(path, "r") as f:
        js = json.load(f)

    # change file
    pos = js
    while True:
        new_target = targets.pop(0)
        if not isinstance(pos, dict):
            break
        if new_target in pos and len(targets) > 0:
            # descend
            pos = pos[new_target]
        elif new_target in pos:
            # delete terminal target
            changed = True
            del pos[new_target]
        else:
            # target not found
            break
        if 0 == len(targets):
            # target not found
            break

    if changed:
        from bleachbit.Options import options

        if options.get("shred"):
            delete(path, True)
        # write file
        with open(path, "w") as f:
            json.dump(js, f)


# Traceback (UnicodeDecodeError)
Błąd: google_chrome.cache: Command to clean JSON file, path=C:\Users\UserXXX\AppData\Local\Google\Chrome\User Data\Default\Preferences, address=dns_prefetching/host_referral_list
Traceback (most recent call last):
File "C:\projects\bleachbit\bleachbit\Worker.py", line 87, in execute
File "C:\projects\bleachbit\bleachbit\Command.py", line 244, in execute
File "C:\projects\bleachbit\bleachbit\FileUtilities.py", line 263, in clean_json
File "C:\Python34\lib\json\__init__.py", line 265, in load
File "C:\Python34\lib\encodings\cp1250.py", line 23, in decode
UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 28203: character maps to <undefined>
Błąd: google_chrome.cache: Command to clean JSON file, path=C:\Users\UserXXX\AppData\Local\Google\Chrome\User Data\Default\Preferences, address=dns_prefetching/startup_list
Traceback (most recent call last):
File "C:\projects\bleachbit\bleachbit\Worker.py", line 87, in execute
File "C:\projects\bleachbit\bleachbit\Command.py", line 244, in execute
File "C:\projects\bleachbit\bleachbit\FileUtilities.py", line 263, in clean_json
File "C:\Python34\lib\json\__init__.py", line 265, in load
File "C:\Python34\lib\encodings\cp1250.py", line 23, in decode
UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 28203: character maps to <undefined>
Błąd: google_chrome.cache: Command to clean JSON file, path=C:\Users\UserXXX\AppData\Local\Google\Chrome\User Data\Default\Preferences, address=net/http_server_properties/servers
Traceback (most recent call last):
File "C:\projects\bleachbit\bleachbit\Worker.py", line 87, in execute
File "C:\projects\bleachbit\bleachbit\Command.py", line 244, in execute
File "C:\projects\bleachbit\bleachbit\FileUtilities.py", line 263, in clean_json
File "C:\Python34\lib\json\__init__.py", line 265, in load
File "C:\Python34\lib\encodings\cp1250.py", line 23, in decode
UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 28203: character maps to <undefined>

# Fixed code
def clean_json(path, target):
    """Delete key in the JSON file"""
    import json

    changed = False
    targets = target.split("/")

    # read file to parser
    with open(path, "r", encoding="utf-8") as f:
        js = json.load(f)

    # change file
    pos = js
    while True:
        new_target = targets.pop(0)
        if not isinstance(pos, dict):
            break
        if new_target in pos and len(targets) > 0:
            # descend
            pos = pos[new_target]
        elif new_target in pos:
            # delete terminal target
            changed = True
            del pos[new_target]
        else:
            # target not found
            break
        if 0 == len(targets):
            # target not found
            break

    if changed:
        from bleachbit.Options import options

        if options.get("shred"):
            delete(path, True)
        # write file
        with open(path, "w", encoding="utf-8") as f:
            json.dump(js, f)