How to return a JSON with special characters in Flask?

Asked

Viewed 222 times

1

When a JSON returns through the methods make_response(jsonify()), my browser can not understand the special characters, how to return JSON in UTF-8 encoding?

Follows the code:

@app.route('/', methods=['GET'])
def index():
    return make_response(jsonify(address_info('Rua Parianas, 665 São Paulo').to_json()), 201)

Answer I would like to receive:

{
  "city": "São Paulo", 
  "country": "Brazil", 
  "lat": -23.5100646, 
  "lng": -46.54488689999999, 
  "neighborhood": "Jardim Jaú (Zona Leste)", 
  "state": "SP", 
  "street_name": "Rua Parianas", 
  "street_number": "665"
}

Answer I’m getting:

{
  "city": "S\u00e3o Paulo", 
  "country": "Brazil", 
  "lat": -23.5100646, 
  "lng": -46.54488689999999, 
  "neighborhood": "Jardim Ja\u00fa (Zona Leste)", 
  "state": "SP", 
  "street_name": "Rua Parianas", 
  "street_number": "665"
}

1 answer

1


The answer to the JSON file is perfectly ok! Special characters are encoded this way for compatibility and security reasons; because of html Escaping (which can bring vulnerabilities to the system) and code page. (In China BIG5 is used, and in Japan SJIS instead of UTF-8 for example..)

When picking up JSON data just treat it the right way, using JSON library or Python codecs to decode it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.