[PYTHON] Eliminate garbled Japanese characters in JSON data acquired by API.

Problems that occurred

When I was practicing to acquire data with the youtube Data API, Japanese characters were garbled when I output the acquired data.

search.py


if __name__ == "__main__":
    try:
        response = youtube_search(
            q='official', part='snippet', type='video', max_count=1, order='date')

        CURRENT_DIR = os.getcwd()
        with codecs.open(CURRENT_DIR + '/' + 'data.json', 'w', encoding='utf-8') as f:
            f.write(json.dumps(response, indent=2))

        print(json.dumps(response, indent=2))

    except HttpError as e:
        print("An HTTP error %d occurred: \n%s" % (e.resp.status, e.content))

result.


########################
##Omit irrelevant parts
########################
        "title": "\u4eee\u9762\u30e9\u30a4\u30c0\u30fc\uff08\u65b0\uff09\uff08\u30b9\u30ab\u30a4\u30e9\u30a4\u30c0\u30fc\uff09\u3000\u7b2c14\u8a71[\u516c\u5f0f]",
        "description": "\u4eee\u9762\u30e9\u30a4\u30c0\u30fc\uff08\u65b0\uff09 \u7b2c14\u8a71\u300c\u30cf\u30a8\u30b8\u30b4\u30af\u30b8\u30f3 \u4eee\u9762\u30e9\u30a4\u30c0\u30fc\u5371\u6a5f\u4e00\u9aea\u300d \u6d0b\u306f\u5bcc\u58eb\u6025\u30cf\u30a4\u30e9\u30f3\u30c9\u3067\u3001\u5148\u8f29\u30fb\u8c37\u6e90\u6b21\u90ce\u3068\u518d\u4f1a\u3002\u3060\u304c\u3001\u3053\u306e\u5730\u4e0b\u306b\u306f\u3001\u30cd\u30aa\u30b7\u30e7\u30c3\u30ab\u30fc\u306e\u602a\u4eba\u30cf\u30a8\u30b8\u30b4\u30af ...",
        "thumbnails": {
          "default": {
            "url": "https://i.ytimg.com/vi/yTFJ-6NnUNY/default.jpg ",
            "width": 120,
            "height": 90
          },
          "medium": {
            "url"

How to solve

The option "ensure_ascii" of json.dumps () is True by default, that is, the setting to execute the process of automatically escaping when it contains ASCII characters is the default. Set this to False to prevent it from being escaped.

search.py


if __name__ == "__main__":
    try:
        response = youtube_search(
            q='official', part='snippet', type='video', max_count=1, order='date')

        CURRENT_DIR = os.getcwd()
        with codecs.open(CURRENT_DIR + '/' + 'data.json', 'w', encoding='utf-8') as f:
            f.write(json.dumps(response, indent=2, ensure_ascii=False))

        print(json.dumps(response, indent=2, ensure_ascii=False))

    except HttpError as e:
        print("An HTTP error %d occurred: \n%s" % (e.resp.status, e.content))

result.


########################
##Omit irrelevant parts
########################
        "title": "Kamen Rider (New) (Sky Rider) Episode 14[official]",
        "description": "Kamen Rider (New) Episode 14 "Venus Flytrap Kamen Rider Close Call" Hiroshi reunites with his senior, Genjiro Tani, at Fuji-Q Highland. However, in this basement, the neo-shocker monster Venus flytrap...",
        "thumbnails": {
          "default": {
            "url": "https://i.ytimg.com/vi/yTFJ-6NnUNY/default.jpg ",

Recommended Posts

Eliminate garbled Japanese characters in JSON data acquired by API.
Eliminate garbled Japanese characters in matplotlib graphs in Cloud Pak for Data Notebook
Eliminate garbled Japanese characters in Python library matplotlib and NetworkX
Export the contents acquired by Twitter Streaming API in JSON format
Fix garbled characters when handling Japanese in Requests
How to eliminate garbled characters in matplotlib output image
Data acquired by Django releted
Display Japanese in JSON file
Resolve garbled Japanese characters in matplotlib of Jupyter Notebook on Docker
Export DB data in json format
Save the audio data acquired by the browser in wav format on the server
Garbled characters when uploading CSV to Google Cloud Storage → Solved by json
Detect Japanese characters from images using Google's Cloud Vision API in Python
How to deal with garbled characters in json of Django REST Framework
Data input / output in Python (CSV, JSON)
[Note] Japanese characters are garbled with atom-runner
Python: Reading JSON data from web API
Get Google Fit API data in Python
Get Youtube data in Python using Youtube Data API
Windows Qt5.4 Python3.4 QProcess Japanese garbled characters
Handles UTF-8 Japanese characters in Python's MySQLdb.