E
E
Emil Revencu2016-01-29 11:43:38
Flask
Emil Revencu, 2016-01-29 11:43:38

How to return euro sign via jsonify (Flask)?

from flask import Flask,jsonify
app = Flask(__name__)
@app.route('/')
def default():
  return jsonify(a=(u"\u20AC").encode('utf8'))

if __name__ == "__main__":
    app.run(debug=True,port=8765)

There is no euro symbol in the response, but a unicode string
How to return the euro sign via jsonify (Flask)?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
pcdesign, 2016-01-29
@Revencu

from flask import Flask, jsonify
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False


@app.route('/')
def default():
    result = u"\u20AC"
    return jsonify(result=result), 200, {'Content-Type': 'text/css; charset=utf-8'}

if __name__ == "__main__":
    app.run(debug=True, port=8767, host='0.0.0.0')

1ac73f5e707649c18e7b68b331b823c1.png

E
Emil Revencu, 2016-01-29
@Revencu

from flask import Flask,jsonify
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False

@app.route('/')
def default():
  return jsonify(a=u"\u20AC".encode('utf8'))

if __name__ == "__main__":
    app.run(debug=True,port=8765)

Here's what happened:
cf84754aed50468386b405baa65d4cac.jpg

A
Arceny, 2016-06-03
@Arceny

json.py

# -*- coding: utf-8 -*-
try:
    from itsdangerous import simplejson as _json
except ImportError:
    from itsdangerous import json as _json
from flask import request
from datetime import datetime
from bson.objectid import ObjectId, InvalidId
from flask.json import dumps
from flask.globals import current_app


class UniversalJSONEncoder(_json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.isoformat()
        if isinstance(obj, ObjectId):
            return str(obj)
        return super(UniversalJSONEncoder, self).default(obj)


def unicode_jsonify(*args, **kwargs):
    indent = None
    if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] and not request.is_xhr:
        indent = 2
    return current_app.response_class(dumps(dict(*args, **kwargs), indent=indent),
                                      mimetype='application/json; charset=utf-8')

app.py
from flask import Response, Flask, Blueprint, abort, request
from .json import unicode_jsonify as jsonify, UniversalJSONEncoder

app = Flask(__name__)
app.json_encoder = UniversalJSONEncoder
app.config['JSON_AS_ASCII'] = False

usage
jsonify({'message': 'Привет, мир'})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question