1

I just started learning JSON, and I want to read a JSON file from my PC.

I tried this with json.loads(), I am getting this error: json.decoder.JSONDecodeError: Expecting ',' delimiter: line 9 column 20 (char 135).

So I tried to load the data from JSON file from my PC , with open(), but I found out that it doesn't return a String type output, and it gives the error: TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper.

Then I tried using read() and also gives the error: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I've tried these:

1)

with open('FILE.json') as f:
        data = json.loads(f.read())

2)

with open('FILE.json') as f:
        data = json.loads(f)

3)

with open('FILE.json', 'r', encoding='utf-8') as f:
          data = json.loads(f.read())
4
  • error Expecting value: line 1 column 1 (char 0) means that you read from empty file. It can't decode empty string. Better check what you have in this file. Or maybe you read from different file than you expect. Commented Jul 12, 2019 at 2:52
  • @furas That just means the file is not formatted correctly. Commented Jul 12, 2019 at 2:55
  • 1
    @furas, @aero blue In this case, the file has already been read. Between each read, the file needs to either be re-opened or need to use f.seek(0) to move back to the start. Commented Jul 12, 2019 at 2:56
  • Your issue is really the first error though line 9 column 20 (char 135). This is indicating there is a formatting error in your JSON file you need to fix, your first example will actually correctly read JSON provided the format is correct. Commented Jul 12, 2019 at 2:58

2 Answers 2

1

Based on reading over the documentation

Try this:

with open(absolute_json_file_path, encoding='utf-8-sig') as f:
    json_data = json.load(f)
    print(json_data)
Sign up to request clarification or add additional context in comments.

Comments

1

You want to use json.load() instead of json.loads()

Example:

 with open(file.json) as f:

      x = json.load(f)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.