CodShot
Python Public

Safe JSON Load

Load JSON data from a file and handle invalid JSON safely.

#python #json #file #error-handling
Python
import json
from pathlib import Path

file_path = Path("settings.json")

try:
    data = json.loads(file_path.read_text(encoding="utf-8"))
    print(data)
except FileNotFoundError:
    print("settings.json was not found")
except json.JSONDecodeError:
    print("settings.json is not valid JSON")

Notes

Use try/except when a file or its content may be missing or invalid.

Snippet actions

CodShot user
CodShot user
Updated: 2026-06-03 15:31
Public snippets
0
Forks
CodShot AI Help
Ask about CodShot features, plans, workspace, AI limits, referrals, and public help topics.
Hi! I can help you understand how CodShot works. What would you like to know?
For account-specific or sensitive issues, please open a support ticket. Open support