Skip to content

gh-59598: Strip leading whitespace in JSONDecoder.raw_decode#153017

Closed
mayank-dev-15 wants to merge 1 commit into
python:mainfrom
mayank-dev-15:fix-json-raw-decode-whitespace
Closed

gh-59598: Strip leading whitespace in JSONDecoder.raw_decode#153017
mayank-dev-15 wants to merge 1 commit into
python:mainfrom
mayank-dev-15:fix-json-raw-decode-whitespace

Conversation

@mayank-dev-15

@mayank-dev-15 mayank-dev-15 commented Jul 4, 2026

Copy link
Copy Markdown

Summary

Fixes #59598

JSONDecoder.raw_decode() raises JSONDecodeError on strings with leading whitespace (e.g., ' {"key": "value"}'), while JSONDecoder.decode() handles it fine. This is inconsistent behavior.

Changes

Add s = s.lstrip() at the start of raw_decode() to strip leading whitespace before decoding, matching the behavior of decode().

Test

import json
decoder = json.JSONDecoder()
# Should work with leading whitespace
result = decoder.raw_decode('  {"key": "value"}')
assert result == ({"key": "value"}, 18)

This matches the behavior of decoder.decode(' {"key": "value"}') which already works correctly.

raw_decode currently ignores leading whitespace before JSON values,
raising JSONDecodeError on strings like '  {"key": "value"}'. This is
inconsistent with JSONDecoder.decode() which handles leading whitespace.

Add s = s.lstrip() at the start of raw_decode to strip leading
whitespace before decoding, matching the behavior of decode().
@bedevere-app

bedevere-app Bot commented Jul 4, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@python-cla-bot

python-cla-bot Bot commented Jul 4, 2026

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@mayank-dev-15 mayank-dev-15 deleted the fix-json-raw-decode-whitespace branch July 4, 2026 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSONDecoder.raw_decode breaks on leading whitespace

1 participant