I am new to Python/Json. My end goal is to load a json file. From the json file info I want to get it into a database.
My thought is to bring the json file in to python and save it as a dictionary.(I'm sure there are quicker/better ways). Then create a database and use the dictionary information to create columns with data.
So, my issue is I am trying to load a json file but I keep getting the following error. I have tried multiple changes/versions to input the file, but I can never succeed.
Here is my code and the errors:
>>> with open("test11.json","r") as file:
... data = json.load(file)
... print(data)
...
Traceback (most recent call last):
File "<python-input-11>", line 2, in <module>
data = json.load(file)
File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/__init__.py", line 293, in load
return loads(fp.read(),
~~~~~~~^^
File "<frozen codecs>", line 325, in decode
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcc in position 24: invalid continuation byte
>>>
Here is my json file(I have use an online json site to verify that my code is valid):
{
"activityId": 18132401113,
"uuidMsb": 817921559470751700,
"uuidLsb": -7059629815279243000,
"name": "Indoor Cycling",
"activityType": "indoor_cycling",
"userProfileId": 1,
"timeZoneId": 10,
"beginTimestamp": 1738148659000,
"eventTypeId": 9,
"rule": "subscribers",
"sportType": "CYCLING",
"startTimeGmt": 1738148659000,
"startTimeLocal": 1738127059000,
"duration": 1554817.9931640625,
"distance": 1000000,
"avgSpeed": 0.643162093569794,
"maxSpeed": 0.7194450200000001,
"avgHr": 110,
"maxHr": 123,
"minHr": 72,
"avgBikeCadence": 85,
"maxBikeCadence": 92,
"calories": 892.47426,
"bmrCalories": 146.6507,
"aerobicTrainingEffect": 2.0999999046325684,
"avgFractionalCadence": 0,
"maxFractionalCadence": 0,
"maxFtp": 228,
"elapsedDuration": 1554817.9931640625,
"movingDuration": 0,
"anaerobicTrainingEffect": 0,
"deviceId": 1,
"minTemperature": 28,
"maxTemperature": 31,
"manufacturer": "GARMIN",
"lapCount": 1,
"waterEstimated": 342,
"trainingEffectLabel": "RECOVERY",
"activityTrainingLoad": 28.710250854492188,
"aerobicTrainingEffectMessage": "MINOR_AEROBIC_BENEFIT_0",
"anaerobicTrainingEffectMessage": "NO_ANAEROBIC_BENEFIT_0",
"moderateIntensityMinutes": 11,
"vigorousIntensityMinutes": 14,
"hrTimeInZone_0": 14446,
"hrTimeInZone_1": 164002,
"hrTimeInZone_2": 898000,
"hrTimeInZone_3": 478370,
"hrTimeInZone_4": 0,
"hrTimeInZone_5": 0,
"decoDive": false,
"purposeful": false,
"autoCalcCalories": false,
"favorite": false,
"pr": false,
"elevationCorrected": false,
"atpActivity": false,
"parent": false
}
I am new to Python/Json. My end goal is to load a json file. From the json file info I want to get it into a database.
My thought is to bring the json file in to python and save it as a dictionary.(I'm sure there are quicker/better ways). Then create a database and use the dictionary information to create columns with data.
So, my issue is I am trying to load a json file but I keep getting the following error. I have tried multiple changes/versions to input the file, but I can never succeed.
Here is my code and the errors:
>>> with open("test11.json","r") as file:
... data = json.load(file)
... print(data)
...
Traceback (most recent call last):
File "<python-input-11>", line 2, in <module>
data = json.load(file)
File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/__init__.py", line 293, in load
return loads(fp.read(),
~~~~~~~^^
File "<frozen codecs>", line 325, in decode
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcc in position 24: invalid continuation byte
>>>
Here is my json file(I have use an online json site to verify that my code is valid):
{
"activityId": 18132401113,
"uuidMsb": 817921559470751700,
"uuidLsb": -7059629815279243000,
"name": "Indoor Cycling",
"activityType": "indoor_cycling",
"userProfileId": 1,
"timeZoneId": 10,
"beginTimestamp": 1738148659000,
"eventTypeId": 9,
"rule": "subscribers",
"sportType": "CYCLING",
"startTimeGmt": 1738148659000,
"startTimeLocal": 1738127059000,
"duration": 1554817.9931640625,
"distance": 1000000,
"avgSpeed": 0.643162093569794,
"maxSpeed": 0.7194450200000001,
"avgHr": 110,
"maxHr": 123,
"minHr": 72,
"avgBikeCadence": 85,
"maxBikeCadence": 92,
"calories": 892.47426,
"bmrCalories": 146.6507,
"aerobicTrainingEffect": 2.0999999046325684,
"avgFractionalCadence": 0,
"maxFractionalCadence": 0,
"maxFtp": 228,
"elapsedDuration": 1554817.9931640625,
"movingDuration": 0,
"anaerobicTrainingEffect": 0,
"deviceId": 1,
"minTemperature": 28,
"maxTemperature": 31,
"manufacturer": "GARMIN",
"lapCount": 1,
"waterEstimated": 342,
"trainingEffectLabel": "RECOVERY",
"activityTrainingLoad": 28.710250854492188,
"aerobicTrainingEffectMessage": "MINOR_AEROBIC_BENEFIT_0",
"anaerobicTrainingEffectMessage": "NO_ANAEROBIC_BENEFIT_0",
"moderateIntensityMinutes": 11,
"vigorousIntensityMinutes": 14,
"hrTimeInZone_0": 14446,
"hrTimeInZone_1": 164002,
"hrTimeInZone_2": 898000,
"hrTimeInZone_3": 478370,
"hrTimeInZone_4": 0,
"hrTimeInZone_5": 0,
"decoDive": false,
"purposeful": false,
"autoCalcCalories": false,
"favorite": false,
"pr": false,
"elevationCorrected": false,
"atpActivity": false,
"parent": false
}
Share
Improve this question
edited 2 days ago
wjandrea
33.1k9 gold badges69 silver badges97 bronze badges
asked Feb 18 at 1:43
WinrolWinrol
93 bronze badges
5
|
2 Answers
Reset to default 0The code is correct but the problem is with the file's encoding.Try converting the file to utf-8 online or use python chardet to automatically detect the right encoding for your json file
import chardet
rawdata = open("test11.json", 'rb').read()
result = chardet.detect(rawdata)
charenc = result['encoding']
with open("test11.json","r", encoding=charenc) as file:
data = json.load(file)
print(data)
See about encoding from this other solution on StackOverflow here
The original file was create using textedit on my mac.
I opened dreamweaver and created a new json file and inserted my data.
I then tried my original code and it worked!!
test11.json
created? Did you create it yourself (perhaps via cut-and-paste), or did you download it from some site, or some other way? – John Gordon Commented Feb 18 at 2:33