m1995 m1995 - 5 months ago 12
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)
json_data.close()
pprint(d)


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

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:

[  
   {  
      "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 re-ran your code and it worked well.

Comments