I am getting a bit of headache just because a simple looking, easy statement is throwing some errors in my face.
I have a json file called strings.json like this:
This works for me.
json.load() accepts file object, parses the JSON data, populates a Python dictionary with the data and returns it back to you.
Suppose JSON file is like this:
{
"emp_details":[
{
"emp_name":"John",
"emp_emailId":"john@gmail.com"
},
{
"emp_name":"Aditya",
"emp_emailId":"adityatest@yahoo.com"
}
]
}
import json
# Opening JSON file
f = open('data.json',)
# returns JSON object as
# a dictionary
data = json.load(f)
# Iterating through the json
# list
for i in data['emp_details']:
print(i)
# Closing file
f.close()
#Output:
{'emp_name':'John','emp_emailId':'john@gmail.com'}
{'emp_name':'Aditya','emp_emailId':'adityatest@yahoo.com'}