title: json.dump - UnicodeDecodeError: 'utf8' codec can't decode byte 0xbf in position 0: invalid start byte
body: <p>I have a dictionary <code>data</code> where I have stored: </p>
  
  <ul>
  <li><p><code>key</code> - ID of an event</p></li>
  <li><p><code>value</code> - the name of this event, where <code>value</code> is a UTF-8 string</p></li>
  </ul>
  
  <p>Now, I want to write down this map into a json file. I tried with this:</p>
  
  <pre><code>with open('events_map.json', 'w') as out_file:
      json.dump(data, out_file, indent = 4)
  </code></pre>
  
  <p>but this gives me the error:</p>
  
  <blockquote>
    <p>UnicodeDecodeError: 'utf8' codec can't decode byte 0xbf in position 0:
    invalid start byte</p>
  </blockquote>
  
  <p>Now, I also tried with:</p>
  
  <pre><code>with io.open('events_map.json', 'w', encoding='utf-8') as out_file:
     out_file.write(unicode(json.dumps(data, encoding="utf-8")))
  </code></pre>
  
  <p>but this raises the same error:</p>
  
  <blockquote>
    <p>UnicodeDecodeError: 'utf8' codec can't decode byte 0xbf in position 0:
    invalid start byte</p>
  </blockquote>
  
  <p>I also tried with:</p>
  
  <pre><code>with io.open('events_map.json', 'w', encoding='utf-8') as out_file:
      out_file.write(unicode(json.dumps(data, encoding="utf-8", ensure_ascii=False)))
  </code></pre>
  
  <p>but this raises the error:</p>
  
  <blockquote>
    <p>UnicodeDecodeError: 'ascii' codec can't decode byte 0xbf in position 3114: ordinal not in range(128)</p>
  </blockquote>
  
  <p>Any suggestions about how can I solve this problem?</p>
  
  <p><strong>EDIT:</strong>
  I believe this is the line that is causing me the problem: </p>
  
  <pre><code>&gt; data['142']
  '\xbf/ANCT25'
  </code></pre>
  
  <p><strong>EDIT 2:</strong>
  The <code>data</code> variable is read from a file. So, after reading it from a file:</p>
  
  <pre><code>data_file_lines = io.open(file_name, 'r', encoding='utf8').readlines()
  </code></pre>
  
  <p>I then do:</p>
  
  <pre><code>with io.open('data/events_map.json', 'w', encoding='utf8') as json_file:
          json.dump(data, json_file, ensure_ascii=False)
  </code></pre>
  
  <p>Which gives me the error:</p>
  
  <blockquote>
    <p>TypeError: must be unicode, not str</p>
  </blockquote>
  
  <p>Then, I try to do this with the data dictionary:</p>
  
  <pre><code>for tuple in sorted_tuples (the `data` variable is initialized by a tuple):
      data[str(tuple[1])] = json.dumps(tuple[0], ensure_ascii=False, encoding='utf8')
  </code></pre>
  
  <p>which is, again, followed by:</p>
  
  <pre><code>with io.open('data/events_map.json', 'w', encoding='utf8') as json_file:
      json.dump(data, json_file, ensure_ascii=False)
  </code></pre>
  
  <p>but again, the same error:</p>
  
  <pre><code>TypeError: must be unicode, not str
  </code></pre>
  
  <p>I get the same error when I use the simple <code>open</code> function for reading from the file:</p>
  
  <pre><code>data_file_lines = open(file_name, "r").readlines()
  </code></pre>