Search code examples
pythonjsonvisual-studio-codeencodingcharacter-encoding

UTF-8 encode not working properly on Greek characters


For some reason when I'm trying to encode a JSON file that I've downloaded from SSMS, the encoding of Greek characters in VS code come out as question marks.

This is my code:

import json

def combine_lines(json_path):
    with open(json_path, 'r', encoding='utf-8-sig') as file:
        json_data = file.read()

    json_data = json_data.replace('\n', '')
    parsed_json = json.loads(json_data)
    formatted_json = json.dumps(parsed_json, indent=4, ensure_ascii=False)
    return formatted_json

json_path = r'D:\jazon.json'
result = combine_lines(json_path)
print(result)

This is an output example:

 {
        "Man_Name": "�� ��� ��� ����",
        "countbar": "977110"
    }

What I'm trying to do is kind of beautify the JSON I have but something goes wrong with the Greek characters (they are printed as question marks). I'm a newbie so there might be an obvious answer that I don't know. Any help would be appreciated!


Solution

  • It seems that the problem was with the output of VS code. I exported the printed JSON into a text file and it seems that the Greek characters are visible.