UnicodeDecodeError while dumping dictionary to file using json dump
<p>I have a dictionary, created from reading the windows registry where keys of dictionary are registry keys and corresponding values are the values of the same key in dictionary. 
Now, I am trying to dump to a file using <code>json.dump()</code>.  But, this gives me two different errors on two different systems. I am unaware about the content of dictionary, but it has unicode values in it. I am opening the file in 'ab' mode to dump data on to same.</p>

<pre><code>with open(file_path, 'ab') as fp:
    json.dump(reg_dict, fp)
</code></pre>

<p>The below errors are observed :-</p>

<pre><code>UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 3: invalid start byte
</code></pre>

<p>And </p>

<pre><code>UnicodeDecodeError: 'utf8' codec can't decode byte 0xf1 in position 21: invalid continuation byte
</code></pre>

<p>Attaching the image for both the errors. I am not sure how can I resolve this, neither why this is coming. Any help will be appreciated.</p>

<p><a href="http://i.stack.imgur.com/s9cYL.png" rel="nofollow"><img src="http://i.stack.imgur.com/s9cYL.png" alt="Error in System 1"></a></p>

<p><a href="http://i.stack.imgur.com/S4UXd.png" rel="nofollow"><img src="http://i.stack.imgur.com/S4UXd.png" alt="Error in System 2"></a></p>

<p>I suppose that encoding of data read from the registry not changed. So it is UTF-16. Specifically, UTF-16LE on one system and UTF-16BE on another. This explains the different errors. If my assumption is correct, this may help you:</p>

<pre><code>import collections
import io
import json

def decode_dict(data):
    if isinstance(data, str):
        return data.decode('utf-16')
    elif isinstance(data, unicode):
        return data
    elif isinstance(data, collections.Mapping):
        return dict(map(decode_dict, data.iteritems()))
    elif isinstance(data, collections.Iterable):
        return type(data)(map(decode_dict, data))
    else:
        return data

with io.open(file_path, 'w', encoding='utf-8') as fh:
    fh.write(json.dumps(decode_dict(reg_dict), ensure_ascii=False))
</code></pre>

python json python-2.7 dictionary