0

I have this JSON file:

[
{
id: "29",
name: "abc",
email: "[email protected]",
data: "2016-05-03"
},
{
id: "17",
name: "ghi",
email: "[email protected]",
data: "2016-05-12"
},
{
id: "12",
name: "qwe",
email: "[email protected]",
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: "[email protected]",data: "2016-05-03"},{id: "17",name: "ghi",email: "[email protected]",data: "2016-05-12"},{id: "12",name: "qwe",email: "[email protected]",data: "2016-04-11"}]
4
  • Are you getting any errors? Commented Jun 16, 2016 at 4:45
  • @AKS Yes. It says that in loads return _default_decoder.decode(s) Commented Jun 16, 2016 at 5:01
  • What are the problems you have? Your question lacks any error description and (apart from a few lines) the minimal example code. Also, have you tried just searching for examples online and adapting them? Commented Jun 16, 2016 at 5:10
  • Small suggestion: don't close files with json_data.close() when you are already inside the with block, as it already closes the file for you at the block ending. Commented Jun 23, 2016 at 13:15

4 Answers 4

2

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":"[email protected]",
      "data":"2016-05-03"
   },
   {  
      "id":"17",
      "name":"ghi",
      "email":"[email protected]",
      "data":"2016-05-12"
   },
   {  
      "id":"12",
      "name":"qwe",
      "email":"[email protected]",
      "data":"2016-04-11"
   }
]

I re-ran your code and it worked well.

Sign up to request clarification or add additional context in comments.

Comments

0

Your json should be like :

[{
    "id": "29",
    "name": "abc",
    "email": "[email protected]",
    "data": "2016-05-03"
}, {
    "id": "17",
    "name": "ghi",
    "email": "[email protected]",
    "data": "2016-05-12"
}, {
    "id": "12",
    "name": "qwe",
    "email": "[email protected]",
    "data": "2016-04-11"
}]

String should be inside quotes. Then only it becomes as valid JSON. You can validate your JSON : Validate JSON

Comments

0

Always validate your json from http://jsonlint.com/ if you get any errors. In your case you are not giving Keys as string.

Comments

0

This is the error:

Traceback (most recent call last): File "C:/Users/L30607/PycharmProjects/untitled1/333.py", line 36, in d = json.load(json_data) File "C:\Python27\lib\json__init__.py", line 291, in load **kw) File "C:\Python27\lib\json__init__.py", line 339, in loads return _default_decoder.decode(s) File "C:\Python27\lib\json\decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python27\lib\json\decoder.py", line 380, in raw_decode obj, end = self.scan_once(s, idx) ValueError: Expecting property name: line 3 column 1 (char 4)

1 Comment

Please do not add errors in the answers section, instead, edit your question and include the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.