title: Python JSON and Unicode
body: <h2>Update :</h2>
  
  <p>I found the answer here : <a href="https://stackoverflow.com/questions/368805/python-unicodedecodeerror-am-i-misunderstanding-encode/370199#370199">Python UnicodeDecodeError - Am I misunderstanding encode?</a> </p>
  
  <p>I needed to explicitly <em>decode</em> my incoming file <em>into</em> Unicode when I read it. Because it had characters that were neither acceptable ascii nor unicode. So the encode was failing when it hit these characters.</p>
  
  <h1>Original Question</h1>
  
  <p>So, I know there's something I'm just not getting here.</p>
  
  <p>I have an array of unicode strings, some of which contain non-Ascii characters.</p>
  
  <p>I want to encode that as json with </p>
  
  <pre><code>json.dumps(myList)
  </code></pre>
  
  <p>It throws an error</p>
  
  <pre><code>UnicodeDecodeError: 'ascii' codec can't decode byte 0xb4 in position 13: ordinal not in range(128)
  </code></pre>
  
  <p>How am I supposed to do this? I've tried setting the ensure_ascii parameter to both True and False, but neither fixes this problem.</p>
  
  <p>I know I'm passing unicode strings to json.dumps. I understand that a json string is meant to be unicode. Why isn't it just sorting this out for me? </p>
  
  <p>What am I doing wrong?</p>
  
  <p>Update : Don Question sensibly suggests I provide a stack-trace. Here it is. : </p>
  
  <pre><code>Traceback (most recent call last):
    File "importFiles.py", line 69, in &lt;module&gt;
      x = u"%s" % conv
    File "importFiles.py", line 62, in __str__
      return self.page.__str__()
    File "importFiles.py", line 37, in __str__
      return json.dumps(self.page(),ensure_ascii=False)
    File "/usr/lib/python2.7/json/__init__.py", line 238, in dumps
      **kw).encode(obj)
    File "/usr/lib/python2.7/json/encoder.py", line 204, in encode
      return ''.join(chunks)
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xb4 in position 17: ordinal not in range(128)
  </code></pre>
  
  <p>Note it's python 2.7, and the error is still occurring with ensure_ascii=False</p>
  
  <p>Update 2 : Andrew Walker's useful link (in comments) leads me to think I can coerce my data into a convenient byte format before trying to json.encode it by doing something like :</p>
  
  <pre><code>data.encode("ascii","ignore")
  </code></pre>
  
  <p>Unfortunately that is throwing the same error. </p>