Search code examples
pythonjsonencodingdecodepython-unicode

JSON Encoding Error While Loading String from a File


After opening and before loading a json file on phython, the code end up getting a string filled with unicode blocks between every character. Its seems to be a encoding problem, any easy way to solve this problem?

import json
import io

# read file
with open('BOVA111618484700 (1).json', 'r',encoding="ASCII") as myfile:
data=myfile.read()

print(data)
# parse file
obj = json.loads(data)

print(data) shows:

[�
�{�
�"�d�a�t�a�h�o�r�a�"�:� �"�2�0�2�1�.�0�4�.�1�5� �1�1�:�0�5�:�0�0�"�,�
�"�m�i�l�i�s�e�c�o�n�d�s�"�:� �"�1�6�1�8�4�8�4�7�0�0�2�3�4�"�,�
�"�b�i�d�"�:� �"�1�1�6�.�3�2�"�,�
�"�a�s�k�"�:� �"�1�1�6�.�3�6�"�,�
�"�l�a�s�t�"�:� �"�1�1�6�.�3�2�"�,�
�"�v�o�l�u�m�e�"�:� �"�1�"�,�
�"�f�l�a�g�s�"�:� �"�2�"�
�}�,�                  #json string continues...

when it should show:

[
{
"datahora": "2021.04.15 11:05:00",
"miliseconds": "1618484700234",
"bid": "116.32",
"ask": "116.36",
"last": "116.32",
"volume": "1",
"flags": "2"
},      #json string continues...

After the print, the json.load function returns this error: JSONDecodeError: Expecting value: line 1 column 2 (char 1)


Solution

  • Thanks @Grismar and @tevemadar the encode of the file was actually "UTF-16 LE" assigning this to the open function solve everything!

    import json
    import io
    
    # read file
    with open('BOVA111618484700 (1).json', 'r',encoding="UTF-16 LE") as myfile:
    data=myfile.read()
    
    print(data)
    # parse file
    obj = json.loads(data)