JSONDecodeError when trying to open a url

Question:
I am trying to open a url using the json module but keep getting an error
Repl link:
https://replit.com/@jonathanessombe/new-attempt#main.py

import json
from urllib.request import urlopen

with urlopen("https://docs.python.org/3/library/urllib.error.html") as response:
    source = response.read()

data = json.loads(source)

print(json.dumps(data, indent=2))
1 Like

The reason is that the data you’re trying to read doesn’t have a JSON structure.

How would you if it has a json structure?

1 Like

Your code should work perfectly fine for cases where it has a JSON structure.