The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →To convert a Python dictionary to ordinary display text, use str(data); for JSON text to exchange with an API, file, or another language, use json.dumps(data). Use repr(data) when you specifically want a debugging-oriented representation. These outputs are different formats, so choose according to how the string will be used.
Choose the right conversion
| Need | Use | Result |
|---|---|---|
| Quick readable display | str(data) |
A Python string representation intended to be fairly human-readable. |
| Debugging representation | repr(data) |
A representation intended to be readable by the interpreter where possible. |
| JSON for an API or data exchange | json.dumps(data) |
JSON-formatted text returned as a Python str. |
| Write JSON straight to a text file | json.dump(data, file) |
JSON written to the file, rather than returned as a string. |
Python’s tutorial distinguishes the functions this way: str() aims for a fairly human-readable representation, while repr() aims for one the interpreter can read where possible. See the Python input and output tutorial.
Convert a dictionary for display or debugging
Use str() for a quick string version of a dictionary:
person = {"name": "Ada", "age": 36}
text = str(person)
print(text)
The resulting string uses Python’s representation of the dictionary; it is not JSON. For debugging, you can request that representation explicitly with repr(person). Neither is the right choice when another system expects JSON.
#1 Best Overall
Convert a dictionary to JSON text
Import Python’s standard-library json module and call json.dumps(). The function returns JSON text as a Python string:
import json
person = {"name": "Ada", "age": 36}
text = json.dumps(person)
For indented output that leaves non-ASCII characters visible in the resulting string, set indent and ensure_ascii:
Rank #2
text = json.dumps(person, indent=2, ensure_ascii=False)
By default, non-ASCII characters are escaped. You can also set sort_keys=True to sort output keys or use separators to control whitespace. The Python json module documentation describes dumps() as serializing an object to a JSON-formatted str.
Write JSON to a file
If you need a JSON file and do not need to keep the serialized text in a variable first, use json.dump() with a text file:
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →import json
person = {"name": "Ada", "age": 36}
with open("person.json", "w", encoding="utf-8") as file:
json.dump(person, file, indent=2, ensure_ascii=False)
The distinction is the destination: dumps() returns a string, while dump() writes to a file. The Python tutorial recommends UTF-8 for JSON files and demonstrates serialization and deserialization in its input and output examples.
What changes when a dictionary becomes JSON?
Dictionary keys become JSON strings
JSON object member names are strings. Python’s JSON encoder converts supported dictionary keys to strings, so the conversion may not preserve the original key types. For example, integer key 1 and string key "1" can both become the JSON member name "1". Such a collision means the encoded representation cannot retain both distinct keys as distinct names. Even without a collision, decoding JSON may produce a dictionary that does not compare equal to the original if it had non-string keys.
Some Python values need an explicit representation
JSON supports a defined set of Python values. A custom class or another unsupported value nested in a dictionary is not converted automatically; json.dumps() can raise TypeError. Decide how that value should be represented in JSON or supply an intentional custom encoder rather than assuming every Python object has a JSON equivalent.
Strict JSON and special floating-point values
By default, json.dumps() permits the non-standard values NaN, Infinity, and -Infinity, because allow_nan defaults to True. For strict JSON compliance, pass allow_nan=False; the encoder then raises ValueError if it encounters an out-of-range float. These options and conversion rules are documented in Python’s JSON encoder reference.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsBest Value
Parse the string using its actual format
For JSON text created with json.dumps(), use json.loads():
restored = json.loads(text)
ast.literal_eval() is for text containing Python literals or container displays, such as a dictionary representation; it is not a general expression evaluator. Python documents that it avoids executing code as eval() does, but warns that malicious or extreme input can still exhaust memory or stack space or consume excessive CPU. Use it only when the input is a Python literal from a trusted source and is appropriately limited. Never use eval() to parse untrusted text because it executes arbitrary code. For JSON input, use json.loads(). See the Python ast documentation for literal_eval() limitations.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




