ujson utf8 content

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
itarill
Posts: 4
Joined: Sat Jul 18, 2020 10:26 pm

ujson utf8 content

Post by itarill » Mon Jul 20, 2020 6:08 pm

I cannot seem to find a good solution to my problem - accented characters in my JSON object. Here is a minimalistic example:

Code: Select all

import ujson
exampleJSON = ujson.loads(
    '{ \n' \
  '"Name": "Á" \n'\
'}\n')

>>> exampleJSON
{'Name': '\xc1'}
>>> print(bytes(ujson.dumps(exampleJSON), 'utf-8'))
b'{"Name": "\xc3\x81"}'
The documentation I found does not detail any encoding options: https://docs.micropython.org/en/latest/ ... ujson.html
Google did not give any relevant hits, maybe my keywords were not useful.

Thanks.

Christian Walther
Posts: 169
Joined: Fri Aug 19, 2016 11:55 am

Re: ujson utf8 content

Post by Christian Walther » Mon Jul 20, 2020 8:47 pm

This looks correct to me. Can you explain what your problem/question is? If you were expecting something different, what is it?

(In case it helps, \xc1 = \u00c1 in a string literal is the character with Unicode code point 0xc1, LATIN CAPITAL LETTER A WITH ACUTE, and \xc3\x81 in a bytes literal is the same thing encoded in UTF-8. loads() and dumps() convert between strings and object structures containing strings, there is no encoding involved.)

Post Reply