Read and Parse JSON from File
Load a JSON file and parse it into a Python object.
Code
Python
import json
with open("data.json", "r", encoding="utf-8") as f:
data = json.load(f)
print(data)Line-by-line explanation
- 1.Import the built-in json module.
- 2.Open the file in read mode with UTF-8 encoding.
- 3.Use json.load() to parse the file contents into a Python dict/list.
- 4.Print the parsed data.
Expected output
{"name": "John", "age": 30}