0

so I am opening a Json file and when I try to load the file to a variable I get an errr because it can't read the file. While I have validated (online) that the Json file is valid. I am using this code:

with open("messagesTest2.json") as json_file:
    data = json.load(json_file) <----- ERROR
    for p in data['commits']:
        print(p['message'])

And I get this error. While I have another json file that is also validated and this code works. But this file doesn't work. My guess is that somewhere in the file there is something that it cannot translate as json? Is the decoder's fault?

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

Do you have any idea how to fix it? Keep in mind that the json file is valid else I'll have to show the file but I'll have to hide some data :D

The Json file (The urls/passwards/logins/etc have been replaced but the format remains the same) :

{
    "commits": [{
        "sha": "asjdaskldjkalsk",
        "node_id": "sakldjaskldjaskldjklas",
        "commit": {
            "author": {
                "name": "korki",
                "email": "[email protected]",
                "date": "2015-09-07T22:06:51Z"
            },
            "committer": {
                "name": "korki",
                "email": "[email protected]",
                "date": "2015-09-07T22:06:51Z"
            },
            "message": "Added LaTex template and instructions",
            "tree": {
                "sha": "askdljaskdlajsklda",
                "url": "https://gits-15.ds.sd.se/04dd5b226dda1915"
            },
            "url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
            "comment_count": 0,
            "verification": {
                "verified": "False",
                "reason": "unsigned",
                "signature": "None",
                "payload": "None"
            }
        },
        "url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
        "html_url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
        "comments_url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
        "author": {
            "login": "korki",
            "id": 999,
            "node_id": "askljdklas==",
            "type": "User",
            "site_admin": "None"
        },
        "committer": {
            "login": "korki",
            "id": 999,
            "node_id": "askljdklas==",
            "type": "User",
            "site_admin": "None"
        },
        "parents": [{
            "sha": "asdaskldjasdklsjl",
            "url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
            "html_url": "https://gits-15.ds.sd.se/04dd5b226dda1915"
        }]
    }, {
        "sha": "kasdjklasdjklas",
        "node_id": "sdklasjdklasjkl",
        "commit": {
            "author": {
                "name": "korki",
                "email": "[email protected]",
                "date": "2015-08-31T10:45:24Z"
            },
            "committer": {
                "name": "korki",
                "email": "[email protected]",
                "date": "2015-08-31T10:45:24Z"
            },
            "message": "Update README.md",
            "tree": {
                "sha": "askldjkasldjklas",
                "url": "https://gits-15.ds.sd.se/04dd5b226dda1915"
            },
            "url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
            "comment_count": 0,
            "verification": {
                "verified": "None",
                "reason": "unsigned",
                "signature": "None",
                "payload": "None"
            }
        },
        "url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
        "html_url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
        "comments_url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
        "author": {
            "login": "korki",
            "id": 999,
            "node_id": "dkasdasdnas==",
            "type": "User",
            "site_admin": "None"
        },
        "committer": {
            "login": "korki",
            "id": 999,
            "node_id": "askldaskldja==",
            "type": "User",
            "site_admin": "None"
        },
        "parents": [{
            "sha": "dlkasdjklas;dlkjas;",
            "url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
            "html_url": "https://gits-15.ds.sd.se/04dd5b226dda1915"
        }]
    }, {
        "sha": "dsagadsgsgdsa",
        "node_id": "sdagfsdgsd",
        "commit": {
            "author": {
                "name": "korki",
                "email": "[email protected]",
                "date": "2015-08-31T10:44:42Z"
            },
            "committer": {
                "name": "korki",
                "email": "[email protected]",
                "date": "2015-08-31T10:44:42Z"
            },
            "message": "Initial commit",
            "tree": {
                "sha": "asdasddasdas",
                "url": "https://gits-15.ds.sd.se/04dd5b226dda1915"
            },
            "url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
            "comment_count": 0,
            "verification": {
                "verified": "None",
                "reason": "unsigned",
                "signature": "None",
                "payload": "None"
            }
        },
        "url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
        "html_url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
        "comments_url": "https://gits-15.ds.sd.se/04dd5b226dda1915",
        "author": {
            "login": "korki",
            "id": 999,
            "node_id": "kjklklj==",
            "type": "User",
            "site_admin": "None"
        },
        "committer": {
            "login": "korki",
            "id": 999,
            "node_id": "jhkjkj==",
            "gravatar_id": "",
            "type": "User",
            "site_admin": "None"
        },
        "parents": []
    }]
}
6
  • Whats the content of messagesTest2.json? Commented Jun 16, 2019 at 14:59
  • Its commits from github. Do you want to see a sample? Commented Jun 16, 2019 at 15:01
  • Can you provide some samples in the messagesTest2.json or at least some basic format. Commented Jun 16, 2019 at 15:04
  • Yeah second. :) Commented Jun 16, 2019 at 15:04
  • @Philipl added json. Commented Jun 16, 2019 at 15:14

2 Answers 2

2

That error means it is reading a blank file. Make sure you are reading the file you think you are reading.

EDIT: Another possibility is that you have already read through all the lines of the file. If you read through all the lines and try to read the file, it will appear as a blank file.

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

6 Comments

What do you mean? I might have spelled wrong the name of the file?
no the name file and everything is correct. Something is missing... Like a wrong encoder? Or I don't kow...
It is finding a file, but the file is blank. So yes, it is possible you have misspelled the filename, and it is now pointing to a blank file. You also might have forgotten to save. There's a bunch of ways this can happen.
I double/triple checked that from the begining. Assume that the file that I read is correct and we should focus on why does it read it as a blank.
Read all the lines? And why doens't it stop? I mean you see my file? You can make a json file of it. And can u check if u still get an errr like that? Or how can I solve that?
|
1

I had the exact same issue. I used a powershell script to create a json file and I tried to read the file from another python script but I kept getting the same error as you, even though the JSON file was properly formatted. The issue was I was using a powershell command, "Out-File" Instead, I used Set-Content and it fixed the issue. I believe it was an encoding difference between the commands. Maybe look at how you created the JSON file and the encoding used. I know this is late but I'll share anyways just in case anyone else is having the same issue.

1 Comment

It will be better if you add a step-by-step procedure.

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.