Winrol
Winrol

Reputation: 9

Loading Json file in to Python "UnicodeDecodeError ... invalid continuation byte"

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
}

Upvotes: 0

Views: 102

Answers (2)

Winrol
Winrol

Reputation: 9

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!!

Upvotes: -1

Moreh wa Chege
Moreh wa Chege

Reputation: 15

The 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

Upvotes: 0

Related Questions