You paste a value printed by a Python script into a JSON config or request body, and the parser stops at the first quote. The data looks close enough: braces, keys, strings. The problem is that Python’s display form for a dictionary is not JSON. The safest fix depends on whether you still have the Python object or only a copied string.
Repair common JSON mistakesWhy Python dict output is not JSON
Python uses single quotes in its usual representation of strings, spells booleans True and False, and uses None for an empty value. JSON requires double-quoted strings and the lowercase literals true, false, and null. A Python dictionary may also contain tuples, sets, dates, or custom objects that have no direct JSON representation.
Python repr · invalid JSON
{'active': True, 'name': 'Mina', 'team': None}JSON · valid
{
"active": true,
"name": "Mina",
"team": null
}A mechanical replacement of apostrophes is not a safe general conversion. An apostrophe can appear inside a string, and Python escape conventions do not map perfectly to JSON. Use the source object whenever possible.
Convert the object before printing it
import json
payload = {"active": True, "name": "Mina", "team": None}
json_text = json.dumps(payload, indent=2)
print(json_text)json.dumps serializes supported Python values into JSON text. It also makes the conversion boundary explicit: a tuple becomes an array, while unsupported custom values raise an error unless you provide a deliberate conversion policy. That error is useful; it prevents silently guessing how your application data should be represented.
When you only have copied text
If the original script is unavailable, first identify the input as Python-style data rather than assuming it is JSON. A repair tool can recognize common patterns such as single quotes, Python boolean literals, and None, but it cannot safely infer every custom representation or recover values omitted from the copy. Treat the result as a draft that needs review.
- 1Keep an untouched copy of the source
- 2Repair only recognized syntax patterns
- 3Parse the result and inspect its values
Never use Python eval to convert untrusted text. Evaluating text can execute Python expressions. Even ast.literal_eval, which avoids arbitrary code execution, only parses Python literals; it does not itself produce JSON or decide how non-JSON values should map.
Verify syntax and meaning separately
A successful parse answers one question: does this text follow JSON syntax? It does not say that a required field is present, that an identifier has the right type, or that null is allowed. Check the parsed structure and validate it against the contract your application expects.
Use the JSON Fixer for common copied-input issues, then inspect the converted shape in the JSON Viewer. If a downstream API requires specific fields, use the JSON Schema Validator. For ordinary JSON formatting, see how to format JSON online; for syntax diagnosis, use the guide to fixing JSON syntax errors.
Choose explicit conversions for Python-only values
JSON has no native date, byte-string, set, or Decimal type. Decide how each one should travel before serialization: an ISO 8601 string for a date, a documented encoding for bytes, or a string/number policy for Decimal. Avoid a broad default conversion that turns unknown objects into their repr; that can produce text which looks meaningful but is not a stable API contract.
Check the receiving boundary too
After conversion, send the text with an appropriate JSON content type and parse it at the receiving side. A payload can be valid JSON and still fail a consumer because it expects an integer where the serializer emitted a string. Keep a small fixture for None/null, booleans, non-ASCII text, and any custom values your application supports.
Tags
- Python dict to JSON
- Python repr
- json.dumps
- single quotes in JSON
- True and None in JSON
- JSON serialization
- malformed JSON
- JSON parsing