title: python byte string encode and decode
body: <p>I am trying to convert an incoming byte string that contains non-ascii characters into a valid utf-8 string such that I can dump is as json.</p>
  
  <pre><code>b = '\x80'
  u8 = b.encode('utf-8')
  j = json.dumps(u8)
  </code></pre>
  
  <p>I expected j to be '\xc2\x80' but instead I get:</p>
  
  <pre><code>UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 0: ordinal not in range(128)
  </code></pre>
  
  <p>In my situation, 'b' is coming from mysql via google protocol buffers and is filled out with some blob data.</p>
  
  <p>Any ideas?</p>
  
  <p>EDIT:
  I have ethernet frames that are stored in a mysql table as a blob (please, everyone, stay on topic and keep from discussing why there are packets in a table). The table collation is utf-8 and the db layer (sqlalchemy, non-orm) is grabbing the data and creating structs (google protocol buffers) which store the blob as a python 'str'. In some cases I use the protocol buffers directly with out any issue. In other cases, I need to expose the same data via json. What I noticed is that when json.dumps() does its thing, '\x80' can be replaced with the invalid unicode char (\ufffd iirc)</p>