Mohammad
Mohammad

Reputation: 1098

read json in Python

I can't believe it's not working! Here is my sample.json file:

{
  "SupportedTests": [
    "example1",
    "example2",
    "example3"
  ],
  "ExecutableFilePath": "C:\\Program Files\\Python37\\python.exe"
}

and the python code for reading this file:


# Opening JSON file
with open('sample.json') as jsonfile:
    jsonfile.seek(0)
    data = json.load(jsonfile)

Here is the error messsage:

raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Any idea why this is not working? It's driving me nuts! Thanks

Upvotes: 0

Views: 63

Answers (1)

D Hudson
D Hudson

Reputation: 1092

Edited to reflect latest question change

Your code is valid and should open and parse the file fine. Note that you don't need jsonfile.seek(0)

The error indicates that the file you've opened is empty - are you sure the path to the file with the JSON is correct?


Original answer

Your json has a comma at the end of this line "ExecutableFilePath": "C:\\Program Files\\Python37\\python.exe",. This is invalid json and won't be parsed.

Because your have this comma, the parser is expecting another key in the json object - hence a string is expected.

Ensure that your JSON is valid with this tool: www.jsonlint.com

Upvotes: 3

Related Questions