0

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

7
  • In your code snippet you didn't include the opening { in the JSON, is it there in the file ? Commented Feb 9, 2021 at 16:49
  • 2
    It was there actually, due to bad formatting it was gone :p Commented Feb 9, 2021 at 16:50
  • @arhr yes I forgot to put it here. still not working. Commented Feb 9, 2021 at 16:50
  • There is a trailing comma in your json Commented Feb 9, 2021 at 16:51
  • 1
    @NidalBarada If the file wasn't there, the open() call would have thrown an exception Commented Feb 9, 2021 at 19:09

1 Answer 1

3

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

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

3 Comments

@D Hudson that was a typo, I fixed it and validate the json file. Same issue exists.
This wouldn't cause the error Expecting value: line 1 column 1 (char 0), would it?
Thanks, no it wouldn't - good point! Answer updated

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.