Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8322863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:31:40+00:00 2026-06-08T23:31:40+00:00

This doesn’t appear to be possible to me using the standard library json module.

  • 0

This doesn’t appear to be possible to me using the standard library json module. When using json.dumps it will automatically escape all non-ASCII characters then encode the string to ASCII. I can specify that it not escape non-ASCII characters, but then it crashes when it tries to convert the output to ASCII.

The problem is – I don’t want ASCII! I just want my JSON string back as a unicode (or UTF-8) string. Are there any convenient ways to do that?

Here’s an example to demonstrate what I want:

d = {'navn': 'Åge', 'stilling': 'Lærling'}
json.dumps(d, output_encoding='utf8')
# => '{"stilling": "Lærling", "navn": "Åge"}'

But of course, there is no such option as output_encoding, so here’s the actual output:

d = {'navn': 'Åge', 'stilling': 'Lærling'}
json.dumps(d)
# => '{"stilling": "L\\u00e6rling", "navn": "\\u00c5ge"}'

So to summarize – I want to convert a Python dict to an UTF-8 JSON string without any escapes. How can I do that?


I’ll accept solutions like:

  • Hacks (pre- and post processing input to dumps to achieve the desired effect)
  • Subclassing the JSONEncoder (I have no idea how it works and the documentation isn’t very helpful)
  • Third party libraries available on PyPi
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:31:41+00:00Added an answer on June 8, 2026 at 11:31 pm

    Requirements

    • Make sure your python files are encoded in UTF-8. Or else your non-ascii characters will become question marks, ?. Notepad++ has excellent encoding options for this.

    • Make sure that you have the appropriate fonts included. If you want to display Japanese characters then you need to install Japanese fonts.

    • Make sure that your IDE supports displaying unicode characters.
      Otherwise you might get an UnicodeEncodeError error thrown.

    Example:

    UnicodeEncodeError: 'charmap' codec can't encode characters in position 22-23: character maps to <undefined>
    

    PyScripter works for me. It’s included with “Portable Python” at http://portablepython.com/wiki/PortablePython3.2.1.1

    • Make sure you’re using Python 3+, since this version offers better unicode support.

    Problem

    json.dumps() escapes unicode characters.

    Solution

    Read the update at the bottom. Or…

    Replace each escaped characters with the parsed unicode character.

    I created a simple lambda function called getStringWithDecodedUnicode that does just that.

    import re   
    getStringWithDecodedUnicode = lambda str : re.sub( '\\\\u([\da-f]{4})', (lambda x : chr( int( x.group(1), 16 ) )), str )
    

    Here’s getStringWithDecodedUnicode as a regular function.

    def getStringWithDecodedUnicode( value ):
        findUnicodeRE = re.compile( '\\\\u([\da-f]{4})' )
        def getParsedUnicode(x):
            return chr( int( x.group(1), 16 ) )
    
        return  findUnicodeRE.sub(getParsedUnicode, str( value ) )
    

    Example

    testJSONWithUnicode.py (Using PyScripter as the IDE)

    import re
    import json
    getStringWithDecodedUnicode = lambda str : re.sub( '\\\\u([\da-f]{4})', (lambda x : chr( int( x.group(1), 16 ) )), str )
    
    data = {"Japan":"日本"}
    jsonString = json.dumps( data )
    print( "json.dumps({0}) = {1}".format( data, jsonString ) )
    jsonString = getStringWithDecodedUnicode( jsonString )
    print( "Decoded Unicode: %s" % jsonString )
    

    Output

    json.dumps({'Japan': '日本'}) = {"Japan": "\u65e5\u672c"}
    Decoded Unicode: {"Japan": "日本"}
    

    Update

    Or… just pass ensure_ascii=False as an option for json.dumps.

    Note: You need to meet the requirements that I outlined at the beginning or else this isn’t going to work.

    import json
    data = {'navn': 'Åge', 'stilling': 'Lærling'}
    result = json.dumps(d, ensure_ascii=False)
    print( result ) # prints '{"stilling": "Lærling", "navn": "Åge"}'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This doesn't seem to happen automatically when using a text area , but does
This doesn't appear to possible either with or without CSS - I want a
This doesn't happen all the time. A bug is not a bug if cannot
This doesn't seem to be as simple as I'd hoped: Using wxWidgets (2.8 stable
This doesn't seem to work in jqTouch or iUI. But I know it's possible
This doesn't seem to work. Is it possible? public interface IInterface1 {} public interface
This doesn't seem to be possible? So what is the best work-around? Expando /
This doesn't seem to be working right now. I'm using Faye with NodeJS behind
This doesn't work: >>> pa = Person.objects.all() >>> pa[2].nickname u'arst' >>> pa[2].nickname = 'something
This doesn't seem to work: $.ajax({ url: http://localhost:3000/foo.json, data: { foo: 'bar' }, headers:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.