Lets process the JSON text that represent student information.
Below student object has attributes id, name and marks
Read JSON file from disk and pass file object to json.load function
import json
with open('student.json') as file_obj:
# read json file from disk and parse to create a dictionary object
student_dict = json.load(file_obj)
Read student attribute name,id,marks
Sudent object which contains address object
{
"id": 1001,
"name": "Dora",
"marks": 90,
"address": {
"street": "383 Madison Ave, New York",
"pincode": 10017
}
}
>>> student_dict["address"] # Read student address
{'street': '383 Madison Ave, New York', 'pincode': 10017}
>>> student_dict['address']['street'] # Read street number
'383 Madison Ave, New York'
Studnet with address and subject object
{
"id": 1001,
"name": "Dora",
"marks": 90,
"address": {
"street": "383 Madison Ave, New York",
"pincode": 10017
},
"subjects": [
"english",
"maths",
"science"
]
}
Read subjects
>>> student_dict['subjects']
['english', 'maths', 'science']
>>> student_dict['subjects'][0]
'english'