prabh
prabh

Reputation: 396

Unable to read a JSON file in my simple python code

I get the below error when i try to read a JSON file, not sure what needs to be corrected:

C:\Users\prabhjot2600\PycharmProjects\PythonPractise\venv\Scripts\python.exe C:/Users/prabhjot2600/Desktop/Prabh/alien_invasion/test.py
Traceback (most recent call last):
  File "C:/Users/prabhjot2600/Desktop/Prabh/alien_invasion/test.py", line 26, in <module>
    data = json.loads(people_string)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 11 column 21 (char 192)

My code is simply reading a JSON file and printing its data. I have checked my JSON data also which looks correct and also formatted it to see if there were any errors left but still cant read the JSON

import json

people_string = '''
{
  "people": [
    {
      "name": "Prabhjot Singh",
      "phone": "9999596310",
      "emails": [
        "[email protected]",
        "[email protected]"
      ],
      "hasLicence": True
    },
    {
      "name": "Sunny Rana",
      "phone": "9999988888",
      "emails": null,
      "hasLicence": False
    }
  ]
}
'''


data = json.loads(people_string)
print(data)

Upvotes: 0

Views: 87

Answers (1)

Boseong Choi
Boseong Choi

Reputation: 2596

JSON stands for JavaScript Object Notation.
So it is subset of Javascript's syntax.
Javascript uses true and false keyword as literal boolean values.
(Whereas python uses True and False).

Therefore, you should use true and false instead of True and False.
See https://www.json.org/json-en.html for detailed syntax.

Upvotes: 1

Related Questions