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
- Empty string: the input has no content.
- Whitespace only: the string contains just spaces, tabs or newlines.
- Non-JSON content: plain text or some other format instead of JSON.
- 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-Typeisapplication/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 stringsinto a Python object.json.dumps(obj)serializes a Python objectobjinto a JSON-encoded string.
Related reading
- json.dumps vs flask.jsonify
- Jupyter Notebook not saving '_xsrf' argument missing from post
- jupyter notebook's kernel keeps dying when I run the code
- JWT 'module' object has no attribute 'encode
- JsonMappingException No suitable constructor found for type [simple type, class ] can not instantiate from JSON object
- Json.Net - Error getting value from 'ScopeId' on 'System.Net.IPAddress
- jython multithreading
- kafka-python consumer not receiving messages
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.