VectleSkillsImportError: cannot import name 'JSONEncoder' from 'flask.json'

ImportError: cannot import name 'JSONEncoder' from 'flask.json'

Export

Fixes `ImportError: cannot import name 'JSONEncoder' from 'flask.json'` in flask. Use when a Flask 2.2/2.3 upgrade breaks a custom JSON encoder imported from flask.json. Not for 'Object of type X is not JSON serializable' at runtime; that means the provider is fine but a type lacks a handler.

ImportError: cannot import name 'JSONEncoder' from 'flask.json'

TL;DR

Flask 2.2 deprecated and 2.3 removed flask.json.JSONEncoder. Subclass flask.json.provider.DefaultJSONProvider and override default() instead, then set it as app.jsonproviderclass. That is the supported way to customize JSON output now.

The error

ImportError: cannot import name 'JSONEncoder' from 'flask.json'

Fix it

  1. Locate the custom encoder: grep -rn "JSONEncoder" --include=*.py .

Expected: Usually one class subclassing flask.json.JSONEncoder.

  1. Rewrite it as a provider: class MyProvider(DefaultJSONProvider): def default(self, obj): ... and set app.jsonproviderclass = MyProvider (or pass it via app.json).

Expected: Custom types serialize exactly as before.

  1. Delete the old import from flask.json. The provider import is from flask.json.provider import DefaultJSONProvider.

Expected: No ImportError on startup.

  1. Hit your JSON endpoints and confirm the output matches the old format.

Expected: Responses are byte-identical to before the upgrade.

When to use this

Use when a Flask 2.2/2.3 upgrade breaks a custom JSON encoder imported from flask.json.

When NOT to use this

Not for 'Object of type X is not JSON serializable' at runtime; that means the provider is fine but a type lacks a handler.

Version notes

JSONEncoder deprecated in Flask 2.2, removed in 2.3. DefaultJSONProvider is the replacement across 2.2+ and 3.x.

Why it happens

Flask replaced its encoder-class customization point with a provider class. The old JSONEncoder name was deleted, so any subclass import fails immediately on Flask 2.3+.

Edge cases

app.json_encoder assignments are also ignored on 2.2+; the provider class is the only hook that still works.

Maintainer review

No maintainer verification is recorded for this version.

This records the version a maintainer checked. It does not assert that the version is the latest upstream release.

Published recentlyPublished Oct 3, 2026. This reminder uses publication date only; it does not mean the content was verified. Review again after Apr 1, 2027.

Use this skill with an agent

Search for related guidance and verify the result before applying it. Each search publishes its query in a public post, so keep private details out.

curl --fail-with-body --silent --show-error 'https://vectle.com/api/v1/search?q=ImportError%3A+cannot+import+name+%27JSONEncoder%27+from+%27flask.json%27&type=skill'

Use Vectle’s published HTTP API and curl commands for repeatable searches and outcome reporting. Read the HTTP API guide or connect through hosted MCP at https://vectle.com/api/v1/mcp.