JSON
Error Handling
Python
JSONDecodeError
Troubleshooting

JSONDecodeError: Expecting value: line 1 column 1 (char 0) in Python

Codemia Knowledge Hub ·

JSONDecodeError: Expecting value: line 1 column 1 (char 0) means Python's json module expected a JSON value at the very start of the input and found nothing usable. The input is almost always empty, whitespace only, or not JSON at all, such as plain text or an unexpected server response. Check what you are actually passing to json.loads() before decoding it.

What the error means

Python's built-in json module encodes and decodes JSON. It raises JSONDecodeError when decoding fails, and "line 1 column 1 (char 0)" says the failure is at the first character: the decoder found no value where the document should begin.

Common causes

  1. Empty string: the input has no content.
  2. Whitespace only: the string contains just spaces, tabs or newlines.
  3. Non-JSON content: plain text or some other format instead of JSON.
  4. Unexpected server response: an API returns an empty body or a different format.

How to fix and prevent it

  • Check the input: make sure the string is not empty and is valid JSON.
  • Validate before decoding: for example, check for a non-empty string before calling json.loads().
  • Log the raw content: when you catch the exception, log the exact response so you can see what arrived.
  • Handle errors: deal gracefully with unexpected or malformed input.

Checks for API responses

  • Status codes: check the HTTP status (200, 404, 500, etc.) to see whether the request succeeded before parsing the body.
  • Content-Type: confirm the response's Content-Type is application/json.
  • Timeouts and retries: set sensible timeouts and retry logic for network or server problems.

json.loads and json.dumps

  • json.loads(s) deserializes a JSON-encoded string s into a Python object.
  • json.dumps(obj) serializes a Python object obj into a JSON-encoded string.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms