To convert a string to a dictionary in Python, use json.loads() when the text is JSON and ast.literal_eval() when it looks like a Python dictionary literal with single quotes. Do not use eval() on text you did not write yourself, because it executes arbitrary code.
Which function to choose depends on where the string came from. JSON from an API or file uses double quotes and true, false and null. A string produced by str(some_dict) uses single quotes and True, False and None.
json.loads()ast.literal_eval()eval()json.loads()json.loads() parses a JSON string into a dictionary (or a list, number or string, depending on the top-level value). It is the right tool for API responses, configuration files and anything else that follows the JSON standard.
JSON requires double quotes around keys and strings. Single quotes, trailing commas and True are not valid JSON, and json.loads() raises json.JSONDecodeError for them:
When you read from a file or HTTP response, use json.load(file) or response.json() and skip the intermediate string.
ast.literal_eval()ast.literal_eval() evaluates a string that contains only Python literals: strings, numbers, tuples, lists, dictionaries, sets, booleans and None. It never calls functions or looks up names, so it is safe for untrusted text in the sense that it cannot run code.
Use it for text that was written by str() or repr() of a dictionary. It keeps Python-only types that JSON cannot express, such as tuples and sets. It raises ValueError or SyntaxError on anything that is not a plain literal:
The Python documentation notes that very large or deeply nested input can still crash the interpreter or use a lot of memory, so apply a size limit to input from outside your system.
eval()eval() runs the string as Python code. If an attacker controls the string, they control your process.
Even when you think the input is trusted, configuration files and environment variables are common injection points. eval() gives no benefit over ast.literal_eval() for data, so there is no reason to use it for this task.
| Input format | Example | Best tool |
|---|---|---|
| JSON | {"a": 1} | json.loads() |
| Python dict literal | {'a': 1} | ast.literal_eval() |
| Query string | a=1&b=2 | urllib.parse.parse_qs() |
| Key=value lines | a=1 per line | split on = or configparser |
| YAML or TOML | config files | tomllib (standard library, Python 3.11+) or a YAML package |
For a flat format you control, plain string methods are enough:
All values are strings, so convert numbers explicitly. split("=", 1) stops at the first =, which keeps values that contain = intact.
Both calls print {'host': 'localhost', 'port': 8080}. The type check matters because both parsers can also return lists, numbers and strings.
json.loads() and ast.literal_eval()?json.loads() follows the JSON standard (double quotes, true, null). ast.literal_eval() follows Python literal syntax (single quotes allowed, True, None, tuples, sets).
Single-quoted keys are not valid JSON. Use ast.literal_eval() if the text is a Python dict, or fix the producer so it emits real JSON, for example with json.dumps().
ast.literal_eval() completely safe?It cannot execute code or call functions, but extremely large or deeply nested input can exhaust memory or crash the parser. Limit the input size.
Use json.dumps(data) for an interchange format, or str(data) for a debug representation.
Use json.loads() for JSON and ast.literal_eval() for Python dictionary literals, and avoid eval() for data. Validate that the result is a dictionary, catch the parse errors, and prefer producing JSON with json.dumps() so the data can be read back reliably.