0

I need to dump Thai language but when me dump don't show I'm use

library

from flask_restful import Api, Resource
from flask import Flask , jsonify
import json

datainjson >>`{"tim": [{"age":19,"gender":"ชาย"}],
        "bill": [{"age": 70,"gender":"male"}]}`

class Helloword(Resource):
    def get(self,name):
        url = './testCode/json/myfilejson.json'
        with open(url, encoding="utf8") as f: 
          names = json.load(f)
        return  json.dumps(names[name],indent=4,ensure_ascii=False)

api.add_resource(Helloword,"/helloword/<string:name>")

this is url >> http://localhost:5000/helloword/tim

output

"[\n    {\n        \"age\": 19,\n        \"gender\": \"\u0e40\u0e1e\u0e28\u0e0a\u0e32\u0e22\"\n    }\n]"

Expect(me need)

[
{
    "age": 19,
    "gender": "เพศชาย"
}
]

But when me use nomall api is ok can show word Thai

 @app.route('/Correct')
 def Correct(): 
  with open(url, encoding="utf8") as f: 
   obj = json.load(f)
  return json.dumps(obj,ensure_ascii=False) 

But I would like to do the first type, I want to check (id) and response value

Thank you

alexis
  • 48,685
  • 16
  • 101
  • 161
  • 1
    Does this answer your question? [Saving utf-8 texts in json.dumps as UTF8, not as \u escape sequence](https://stackoverflow.com/questions/18337407/saving-utf-8-texts-in-json-dumps-as-utf8-not-as-u-escape-sequence) – Aziz Sonawalla Sep 07 '20 at 16:00
  • Both are valid JSON representations of the same data. If you decode the JSON the strings will be readable characters again. – Klaus D. Sep 07 '20 at 16:35
  • Looks like you are converting to string twice: `json.dumps()` turns your dictionary into a string, and your final output is that string -- with all the quotes and brackets escaped. Try returning just `names[name]` (as a dictionary) instead. – alexis Sep 07 '20 at 20:16
  • thank Aziz and alexis. for very good advice :) – NuttPong Anupat Sep 18 '20 at 20:42

0 Answers0