Home  >  Article  >  Backend Development  >  An error occurred while parsing the JSON file, possibly a hidden value in the JSON content

An error occurred while parsing the JSON file, possibly a hidden value in the JSON content

WBOY
WBOYforward
2024-02-06 08:39:111241browse

解析 JSON 文件时出错,可能是 JSON 内容的隐藏值

Question content

I have this JSON file:

https://drive.google.com/file/d/1zh_fJJNWs9GaPnlLZ459twSubsYkzMi5/view?usp=share_link

Looks normal at first, even using the online json schema validator However, when parsing it locally, I get an error. I tried with python, nodejs and golang but it doesn't work. I think it might have some hidden value that makes it impossible to parse it


Correct answer


This is the complete solution. Comments added to the code.

# read the file as bytes
import chardet
import json
file_path=r"2022_2973.json"
with open (file_path , "rb") as f:
    data= f.read() # read file as bytes
file_encoding=chardet.detect(data)['encoding'] # detect the encoding
print(f"file(bytes) encoding:{file_encoding}") # print encoding

json_data = json.loads(data.decode(file_encoding)) # decode the bytes and load the json data
json_data['snaps'][1]

Output:

file(bytes) encoding:UTF-16
{'group': 'Slot',
 'group_order': 1,
 'positions': [{'group': 'Slot',
   'position': 'SLWR',

The above is the detailed content of An error occurred while parsing the JSON file, possibly a hidden value in the JSON content. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete