JSONRepair

How to Fix Python's JSONDecodeError: Expecting Value at Line 1 Column 1

You ran the script, everything looked fine, and Python answered with json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0). Here is how to fix it: the parser wanted a JSON value at the very first character and found nothing it could use, so the problem is the input, not the parser.

The full traceback is short and almost useless:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The traceback names the exact failure point: character 0, the first character of whatever you handed the parser. It expected a value there, an opening { or [, a quote, a digit, true, false, or null, and found none of those. Every cause below is a different answer to the same question: why does your input start wrong?

1. The input is empty

This is the cause I would bet on first. An empty string, an empty file, or an HTTP response with no body all raise exactly this error:

import json
json.loads("")   # JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Empty responses are more common than people expect. A 204 No Content reply, a server that died mid-request, a file truncated to zero bytes by a crashed writer, a queue message that never got a payload. The parser is doing its job; there was simply nothing to parse.

The fix is a guard before the parse, not a bigger try/except around it:

import json

def safe_loads(text, fallback=None):
    if not text or not text.strip():
        return fallback
    return json.loads(text)

data = safe_loads(response.text, fallback={})

An empty body is something APIs do routinely, so I treat it as a normal case with a default, and reserve the exception handler for input that is present but broken.

2. The response is not JSON at all

A wrong URL, an expired session, a server error page: each of these returns HTML, and HTML fails at char 0 too, because < is not a valid start for any JSON value. Python reports the same Expecting value message it uses for empty input, which is why this one misleads people.

Before you parse any new response, look at what you actually received:

print(repr(response.text[:200]))

When the output starts with <!DOCTYPE html> or an nginx error page, the mystery is over and the bug is upstream of the parser. Calling response.raise_for_status() before .json() turns this confusing parse error into the real HTTP error, and checking the Content-Type header catches the case where the endpoint silently changed what it returns.

3. An invisible character is sitting at the front

Files saved by some Windows editors begin with a UTF-8 byte order mark, \ufeff, which renders as nothing in most editors. Python's json module does not skip it, so a file that looks perfectly valid fails at char 0:

with open("data.json", encoding="utf-8") as f:
    data = json.load(f)  # JSONDecodeError if the file starts with a BOM

Open the file with utf-8-sig instead, which strips the BOM automatically:

with open("data.json", encoding="utf-8-sig") as f:
    data = json.load(f)

If the text comes from a network response rather than a file, text.lstrip("\ufeff") before parsing does the same job. This cause is worth knowing about precisely because the file looks fine everywhere you inspect it. For the full treatment of invisible characters, see the BOM parse error guide.

4. You are parsing the wrong thing

This sounds insulting and I include it because it has burned me more than once. A path typo so you opened an empty sibling file. A variable reassigned between the request and the parse. response.text on a download that was a binary file all along. Print repr() of the exact object you pass to json.loads, not the thing you think you are passing. The number of times the "invalid JSON" turned out to be a perfectly valid something-else is higher than I would like to admit.

What to do when the cause still is not obvious

When the guard does not explain it, catch the exception and make it show you what it saw. JSONDecodeError carries the evidence: e.doc is the full input string and e.pos is the character where parsing gave up.

try:
    data = json.loads(text)
except json.JSONDecodeError as e:
    print(f"Failed at char {e.pos}: {e.doc[max(0, e.pos-20):e.pos+20]!r}")
    raise

Seeing the raw bytes around the failure point makes the cause obvious in one look, which is more than the bare message ever gives you. I keep this snippet in every project that ingests JSON from outside.

One adjacent error worth knowing, since people confuse the two: if the message says Expecting property name enclosed in double quotes instead of Expecting value, you are parsing a Python dict repr with single quotes, not JSON. Different error, different fix, covered in the single quotes guide.

My standing rule is simple: never parse a new data source sight unseen. Print the first 200 characters of it once, the day you wire it up. The parser reports where it gave up; the input is what put it there.

Paste your broken JSON and get valid JSON back instantly, with a plain-English list of every fix.

Fix My JSON Now

Frequently asked questions

Why does requests.get().json() raise JSONDecodeError: Expecting value?

The response body was empty or was not JSON at all, usually an HTML error page from a wrong URL, an expired session, or a server error. Check response.status_code and print repr(response.text[:200]) before calling .json().

Does json.loads accept bytes?

Yes. Since Python 3.6, json.loads accepts str, bytes, and bytearray, detecting the encoding automatically. A bytes object raises this error only when it is empty, whitespace-only, or starts with an invalid character such as a BOM.

What is the difference between "Expecting value" and "Expecting property name enclosed in double quotes"?

"Expecting value" at char 0 means the input starts with nothing usable: it is empty, HTML, or begins with an invisible character. "Expecting property name enclosed in double quotes" means parsing started fine and then hit single quotes, usually a Python dict repr pasted where JSON was expected.

Can Python parse JSON with single quotes?

Not with json.loads: single-quoted strings are not valid JSON. If the text is genuinely a Python literal, ast.literal_eval can parse it, but the correct fix is to produce real JSON with json.dumps instead of calling str() on a dict.

How do I see what the parser actually received?

Catch json.JSONDecodeError and inspect its attributes: e.doc is the full input string and e.pos is the character where parsing failed. Printing repr(e.doc[max(0, e.pos-20):e.pos+20]) shows the raw bytes around the failure point.

Related reading: "Unexpected End of JSON Input": Empty Body, Cut-Off Payload, or Unclosed Bracket · Unexpected Token '<' in JSON at Position 0: Your API Returned HTML, Not JSON · Why Is My JSON Invalid? The 7 Syntax Errors That Break JSON.parse