-1

I have the following json data in a file that I am trying to read in python but I am getting this error. What am I doing wrong?

[
    {
        "name": "Alex C",
        "age": 2,
        "city": "Houston"
    },
    {
        "name": "John G",
        "age": 40,
        "city": "Washington"
    },
    {
        "name": "Bala T",
        "age": 22,
        "city": "Bangalore"
    }
]

Here is my code:

JFile = "JData.json"
F = open(JFile, "w")
try:
    proc = subprocess.Popen([MyCMD], shell=True, encoding='utf-8', stdout=F)
except Exception as ex:
    print("ERROR: CMD Failed....", ex)
F.close()
try:
    with open(JFile, 'r', encoding='utf-8') as J:
        JData = json.loads(J.read())
except json.decoder.JSONDecodeError as e:
    print("invalid json", e)

When I try to run this I get this error:

invalid json Expecting value: line 1 column 1 (char 0)

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Salton
  • 199
  • 2
  • 9
  • I got no error. Make sure your file is not empty. – Polatkan Polat Jan 22 '23 at 01:06
  • yup. it's not empty. I double-checked. it is not empty. Thanks for checking – Salton Jan 22 '23 at 06:40
  • Apparently `MyCMD` writes invalid JSON to the file. – mkrieger1 Jan 22 '23 at 10:02
  • I got it. the issue is that it is opening the file before it finishes creating it. that is why it's is giving this error. so by adding proc.wait() resolves the issue – Salton Jan 22 '23 at 17:02
  • Also `shell=True` is wrong here. It works on Windows for obscure reasons but you have no reason to need a shell here. See also [Actual meaning of `shell=True` in `subprocess`](https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess) – tripleee Jan 22 '23 at 20:42

4 Answers4

1

That error suggests the interpreter thinks your data is not valid JSON, despite the fact that the sample you provided is valid JSON. The code you provided works as intended in my local interpreter (Windows 10, Python 3.9.5, standard json library).

I would double check that the JFile variable is in fact pointing at the file you think it is and that there is no additional data in the file before your JSON (leading whitespace should be acceptable). If that checks out I would try re-saving your file to ensure it really is utf-8 encoded or try reading it without explicitly setting encoding='utf-8' in your code. I think an encoding mismatch would throw a different error but nothing is obviously wrong so it's worth checking.

  • Thanks for your help. I thought it was an encoding miss match but I confirmed it is ok. I add code to how I am generating the json file. if I am manually placing the file and removing the code above that is saving the file then it works. I am new to this. maybe it is obvious – Salton Jan 22 '23 at 06:29
0

what does JFile look like? is it a relative path to the file containing your json object?

  1. try using the full path (don't forget the file extension).
  2. make sure you that the python interpreter has permission to read that file.
Mahmoud
  • 9,729
  • 1
  • 36
  • 47
0

The error Expecting value: line 1 column 1 (char 0) is thrown when json.load receives an empty string. Have you double checked if the file you're trying to read contains what you think it does and is in the correct directory? Assuming a file containing a valid JSON, your code should work.

Priw8
  • 1
  • 1
0

Actually, the json file was not yet created when I was trying to read in the code. I had to add proc.wait() to resolve my issue.

JFile = "JData.json"
F = open(JFile, "w")
try:
    proc = subprocess.Popen([MyCMD], shell=True, encoding='utf-8', stdout=F)
    proc.wait()
except Exception as ex:
    print("ERROR: CMD Failed....", ex)
F.close()
try:
    with open(JFile, 'r', encoding='utf-8') as J:
        JData = json.loads(J.read())
except json.decoder.JSONDecodeError as e:
    print("invalid json", e)
Salton
  • 199
  • 2
  • 9