-1

I have a script that receives an input from a 3rd party API:

import requests

url = "https://APIURL"

headers = {
    'x-rapidapi-host': "APIHOST",
    'x-rapidapi-key': "APIKEY"
    }

response = requests.request("GET", url, headers=headers)
print (response.text)

OUTPUT:

[{"name":"Afghanistan","topLevelDomain":[".af"],"alpha2Code":"AF","alpha3Code":"AFG","callingCodes":["93"],"capital":"Kabul","altSpellings":["AF","Afġānistān"],"region":"Asia","subregion":"Southern Asia","population":26023100,"latlng":[33.0,65.0],"demonym":"Afghan","area":652230.0,"gini":27.8,"timezones":["UTC+04:30"],"borders":["IRN","PAK","TKM","UZB","TJK","CHN"],"nativeName":"افغانستان","numericCode":"004","currencies":["AFN"],"languages":["ps","uz","tk"],"translations":{"de":"Afghanistan","es":"Afganistán","fr":"Afghanistan","ja":"アフガニスタン","it":"Afghanistan"},"relevance":"0"}, etc.]

DESIRED OUTPUT:

[
    {
        "name": 
            "Afghanistan",
        "topLevelDomain": 
            [".af"],
        "alpha2Code":
            "AF",
        "alpha3Code":
            "AFG",
        "callingCodes":
            ["93"],
        "capital":
            "Kabul",
        "altSpellings":
            [
                "AF",
                "Afġānistān"
            ],
        "region":
            "Asia",
        "subregion":
            "Southern Asia",
        "population":
            26023100,
        "latlng":
            [33.0,65.0],
        "demonym":
            "Afghan",
        "area":
            652230.0,
        "gini":
            27.8,
        "timezones":
            [
                "UTC+04:30"
            ],
        "borders": 
            [
                "IRN",
                "PAK",
                "TKM",
                "UZB",
                "TJK",
                "CHN"
            ],
        "nativeName":
             "افغانستان",
        "numericCode":
             "004",
        "currencies":
             [
                 "AFN"
             ],
        "languages":
             [
                  "ps",
                  "uz",
                  "tk"
             ],
        "translations":
             {
                 "de":
                      "Afghanistan",
                 "es":
                      "Afganistán",
                 "fr":
                      "Afghanistan",
                 "ja":
                      "アフガニスタン",
                 "it":
                      "Afghanistan"
             },
        "relevance":
             "0"
    },
etc.]

So, I check stackoverflow and find THIS solution to be applied:

import requests
import json
  
    url = "https://APIURL"
    
    headers = {
        'x-rapidapi-host': "APIHOST",
        'x-rapidapi-key': "APIKEY"
        }
    
    response = requests.request("GET", url, headers=headers)
    print ( json.dumps(response.text, sort_keys=True, indent=4) )

But then the OUTPUT isn't what I expected. Just messes it up with trying to encode languages such as japanese:

"[{"name":"Afghanistan","topLevelDomain":[".af"],"alpha2Code":"AF","alpha3Code":"AFG","callingCodes":["93"],"capital":"Kabul","altSpellings":["AF","Af\u0121\u0101nist\u0101n"],"region":"Asia","subregion":"Southern Asia","population":26023100,"latlng":[33.0,65.0],"demonym":"Afghan","area":652230.0,"gini":27.8,"timezones":["UTC+04:30"],"borders":["IRN","PAK","TKM","UZB","TJK","CHN"],"nativeName":"\u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646","numericCode":"004","currencies":["AFN"],"languages":["ps","uz","tk"],"translations":{"de":"Afghanistan","es":"Afganist\u00e1n","fr":"Afghanistan","ja":"\u30a2\u30d5\u30ac\u30cb\u30b9\u30bf\u30f3","it":"Afghanistan"},"relevance":"0"},

What am I missing here?

fishmong3r
  • 1,414
  • 4
  • 24
  • 51

2 Answers2

2

Use the .json() method of the response and ensure_ascii if you want to see non-ASCII characters correctly. .json() will process the data into a Python data structure, which is needed for .dumps().

response = requests.request("GET", url, headers=headers)
print(json.dumps(response.json(), indent=4, ensure_ascii=False))
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
1

pass ensure_ascii argument to json.dumps()

print(json.dumps(data, indent=4, ensure_ascii=False))

Output:

[
    {
        "name": "Afghanistan",
        "topLevelDomain": [
            ".af"
        ],
        "alpha2Code": "AF",
        "alpha3Code": "AFG",
        "callingCodes": [
            "93"
        ],
        "capital": "Kabul",
        "altSpellings": [
            "AF",
            "Afġānistān"
        ],
        "region": "Asia",
        "subregion": "Southern Asia",
        "population": 26023100,
        "latlng": [
            33,
            65
        ],
        "demonym": "Afghan",
        "area": 652230,
        "gini": 27.8,
        "timezones": [
            "UTC+04:30"
        ],
        "borders": [
            "IRN",
            "PAK",
            "TKM",
            "UZB",
            "TJK",
            "CHN"
        ],
        "nativeName": "افغانستان",
        "numericCode": "004",
        "currencies": [
            "AFN"
        ],
        "languages": [
            "ps",
            "uz",
            "tk"
        ],
        "translations": {
            "de": "Afghanistan",
            "es": "Afganistán",
            "fr": "Afghanistan",
            "ja": "アフガニスタン",
            "it": "Afghanistan"
        },
        "relevance": "0"
    }
]
deadshot
  • 8,881
  • 4
  • 20
  • 39
  • I upvoted back. :) thanks for the comment, this definitely solves the character encoding issue, but not the format of the output. – fishmong3r Sep 01 '20 at 18:15