m1995 m1995 - 1 year ago 83
JSON Question

Using python to read an array from a JSON file

I have this JSON file:

id: "29",
name: "abc",
email: "def@school.edu",
data: "2016-05-03"
id: "17",
name: "ghi",
email: "jkl@school.edu",
data: "2016-05-12"
id: "12",
name: "qwe",
email: "cde@school.edu",
data: "2016-04-11"

I wrote this script:

with open('C:/Users/L30607/Desktop/FYP/FourthMay-AllStudents-stackoverflow.json') as json_data:
d = json.load(json_data)

How do I parse the file and extract the array?

I want to get:

d = [{id: "29",name: "abc",email: "def@school.edu",data: "2016-05-03"},{id: "17",name: "ghi",email: "jkl@school.edu",data: "2016-05-12"},{id: "12",name: "qwe",email: "cde@school.edu",data: "2016-04-11"}]

Answer Source

Your JSON is not formatted properly. I put validated it in https://jsonformatter.curiousconcept.com/ and it shows that your keys are not wrapped in quotes. This is what it should be:


I re-ran your code and it worked well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download