1

Is there a way to save a Unicode string into JSON that allows for Unicode codepoints to be replaced with their actual characters?

For instance, having a dict like this ported into JSON...:

dict1[u'N\u00e1utico'] = 2

...instead of having it dumped with the codepoint, could the key be dumped as the actual string?:

Náutico

Printing works fine for representing the characters, but saving I'm just lost on. Thanks.

user1549620
  • 123
  • 2
  • 7
  • 2
    A more important question is whether JSON allows this, and whoever is supposed to read the JSON output supports it. –  Aug 15 '12 at 21:31
  • It's going to be ported to a database then used online most likely – user1549620 Aug 15 '12 at 22:31

2 Answers2

4

Any library that writes JSON is going to provide the unicode codepoint for characters that fall outside of the standard ASCII range, and any library that can read JSON (including browsers) are going to display it correctly. I'm not certain why you think you need the accented character when the string is represented in JSON, but you shouldn't, and as an interchange format providing the codepoint is the correct behavior.

g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
  • 2
    Having readable JSON might be handy for debugging purposes, but as an interchange format I presume it was designed to assume non-ASCII characters could be destroyed somewhere during transport. – Mark Ransom Aug 15 '12 at 21:49
  • The endgame of this would be porting the JSON to a database to be used most likely online. I was just wondering if there was a way that it was possible, otherwise it would be up to whatever outputter to be able to read it properly. Your explanation was really helpful in this respect, thanks! – user1549620 Aug 15 '12 at 22:23
1

Do you mean include non-ASCII characters as raw characters, rather than the equivalent \u escapes? If so:

>>> print json.dumps({u'N\u00e1utico': 2}, ensure_ascii= False)
{"Náutico": 2}
bobince
  • 528,062
  • 107
  • 651
  • 834
  • That's a good answer as well, and for readability in debugging it's worth knowing about. For what he's describing as a use case though he really doesn't need the raw values. Any web app or browser worth anything will load and display it correctly when it becomes necessary down the road. – g.d.d.c Aug 16 '12 at 01:57