To read a JSON file in Python, you can use the built-in json
module. Here is a sample file.json
file:
{
"name": "John Doe",
"age": 32,
"address": {
"street": "123 Main St",
"city": "Anytown",
"state": "CA"
},
}
And here is some sample Python code for reading this file:
import json
with open('file.json', 'r') as json_file:
data = json.load(json_file)
print(data["name"])
# Output: John Doe
The json.loads
method takes in a JSON string and converts it into a Python dictionary. You can read more about the JSON library in the
official Python docs
.