Skip to content

Conversation

@AndreyLebedev345
Copy link

Add a new json_safe() method that provides graceful error handling when parsing JSON responses. This method offers several advantages over the standard json() method:

  • Returns a configurable default value instead of raising exceptions
  • Optionally checks HTTP status before parsing (raise_for_status parameter)
  • Handles empty responses gracefully
  • Catches JSONDecodeError, UnicodeDecodeError, and ValueError

This is particularly useful for:

  • APIs that may return malformed JSON
  • Scenarios requiring default fallback values
  • Graceful degradation in error conditions
  • Rate limiting and service unavailability handling

Example usage:
data = response.json_safe(default={}) data = response.json_safe(default=[], raise_for_status=False)

🤖 Generated with Claude Code

Summary

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

Add a new json_safe() method that provides graceful error handling when
parsing JSON responses. This method offers several advantages over the
standard json() method:

- Returns a configurable default value instead of raising exceptions
- Optionally checks HTTP status before parsing (raise_for_status parameter)
- Handles empty responses gracefully
- Catches JSONDecodeError, UnicodeDecodeError, and ValueError

This is particularly useful for:
- APIs that may return malformed JSON
- Scenarios requiring default fallback values
- Graceful degradation in error conditions
- Rate limiting and service unavailability handling

Example usage:
  data = response.json_safe(default={})
  data = response.json_safe(default=[], raise_for_status=False)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant