0

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
}
5
  • There seems to be something wrong with your file named test11.json. Your code works perfectly on my machine. I would try copying the json from your stack overflow question, writing it to a new file, saving it, and reading from that. Another possible issue is that whatever text editor you're using isn't actually saving the file as utf-8 which python is expecting. I have had this issue personally on a windows device. Try using VSCode, and set it to save as UTF-8. I don't think your problem is in your code and it's logic Commented Feb 18 at 2:00
  • 2
    Try specifying an encoding like cp1252 or latin-1 Commented Feb 18 at 2:11
  • 1
    How was the file 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? Commented Feb 18 at 2:33
  • 1
    The character Ì (0xCC) does not exist in the data shown in the question. Your file was almost certainly created on a system (probably Windows) using cp1252 (ISO-8859-1) encoding. If you specify the correct encoding when opening the file you'll have more success. Commented Feb 18 at 8:52
  • Related: UnicodeDecodeError, invalid continuation byte Commented Feb 19 at 1:05

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

-1

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

2 Comments

That's intriguing. How did you use textedit and end up with a file with a .json extension?
opened an json file pasted in code then saved it and changed extension. Alot! Done wrong in that sentance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.