---
title: "Convert Unicode string to UTF-8, and then to JSON"  
description: "Convert Unicode string to UTF-8, and then to JSON"  
author: "Anonymous User"  
published: 2013-06-20  
updated: 2013-06-20  
canonical: https://www.mindstick.com/forum/1243/convert-unicode-string-to-utf-8-and-then-to-json  
category: "json"  
tags: ["json"]  
reading_time: 2 minutes  

---

# Convert Unicode string to UTF-8, and then to JSON

Hi [Developers](https://www.mindstick.com/articles/12875/blunders-you-must-avoid-while-hiring-java-developers-to-get-the-best-fulfilling-your-needs), \
\
I want to [encode](https://www.mindstick.com/blog/51/encode-and-decode-in-dot-net) a [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) in UTF-8 and view the corresponding UTF-8 bytes individually. In the [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python) REPL the following seems to work fine:\
\
>>> [unicode](https://www.mindstick.com/blog/49/unicode-character-string-datatypes)('©', 'utf-8').encode('utf-8')'\xc2\xa9'\
\
Note that I’m using U+00A9 [COPYRIGHT](https://www.mindstick.com/news/3650/authors-who-contribute-to-training-ai-models-join-copyright-lawsuit-against-openai-microsoft) [SIGN as](https://answers.mindstick.com/qa/34449/which-british-redhead-did-paris-match-sign-as-a-regular-writer-in-1996) an example here. The '\xC2\xA9' looks close to what I want — a string consisting of [two separate](https://www.mindstick.com/forum/159375/how-to-communicate-with-two-separate-database-in-mssql-server) code points: U+00C2 and \
\
U+00A9. (When UTF-8-decoded, it gives back the original string, '\xA9'.)\
\
Then, I want the UTF-8-encoded string to be converted to a JSON-compatible string. However, the following doesn’t seem to do what I want:\
\
>>> [import](https://www.mindstick.com/blog/294/how-to-import-or-export-sql-server-table-data-in-ms-excel-sheet-using-c-sharp-code) json; json.dumps('\xc2\xa9')'"\\u00a9"'\
\
Note that it generates a string containing U+00A9 (the original symbol). Instead, I need the UTF-8-encoded string, which would look like "\u00C2\u00A9" in valid JSON.\
\
TL;DR How can I turn '©' into "\u00C2\u00A9" in Python? I feel like I’m [missing](https://answers.mindstick.com/qa/52138/international-missing-children-s-day-2019-was-observed-on) something obvious — is there no built-in way to do this?\

## Replies

### Reply by AVADHESH PATEL

Hi Babe,\
If you really want "\u00c2\u00a9" as the output, give json an Unicode string as input.\
**>>> print json.dumps(u'\xc2\xa9')"\u00c2\u00a9"**\
You can generate this Unicode string from the raw bytes:\
**s = unicode('©', 'utf-8').encode('utf-8')****s2 = u''.join(unichr(ord(c)) for c in s)**\
I think what you really want is "\xc2\xa9" as the output, but I'm not sure how to generate that yet.


---

Original Source: https://www.mindstick.com/forum/1243/convert-unicode-string-to-utf-8-and-then-to-json

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
