String to Dictionary in Python
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Converting a string to a Python dictionary depends on what the string actually contains. A JSON payload, a Python literal, and a query-string fragment look similar at a glance, but they require different parsers. The safest solution is to choose the parser that matches the input format exactly and avoid eval.
Parse JSON with json.loads
If the string comes from an API or serialized data interchange, it is probably JSON. In that case, use json.loads.
This is the correct path for standards-compliant JSON. Note that JSON uses true, false, and null, not Python's True, False, and None.
Parse Python Literal Syntax with ast.literal_eval
Sometimes the string is not JSON at all, but a Python dictionary literal copied from logs or internal code. In that case, json.loads fails because the syntax is different.
ast.literal_eval is much safer than eval because it only accepts Python literal structures such as strings, numbers, lists, tuples, dicts, booleans, and None.
Handle Query Strings and Key-Value Text Separately
If the source string looks like name=Ada&score=9, it is not a dictionary literal. It is a URL-encoded query string and should be parsed accordingly.
This returns lists of values because query-string keys may repeat. If you need a simple one-value-per-key dictionary, flatten it carefully:
Choosing the right parser up front avoids a lot of brittle post-processing.
Never Use eval for This
eval can execute arbitrary code. That means an input string is no longer "data", it becomes executable source. For untrusted input, this is a security bug.
Bad pattern:
Even for trusted internal tools, eval is almost always the wrong abstraction because it accepts far more than you need.
Validate the Resulting Structure
Parsing is only the first step. After converting the string, validate the shape and types before using the result. A string that parses successfully can still be missing required keys or contain the wrong value types.
This is especially important when the parsed dictionary drives downstream logic such as configuration or access control.
A practical parser should also fail clearly. If you expect JSON, raise a JSON-specific error instead of silently trying unrelated formats, because silent fallback hides bad inputs and makes debugging much harder later.
Common Pitfalls
- Using
json.loadson a Python literal string with single quotes andTrue. - Using
ast.literal_evalon actual JSON and then wondering why booleans behave differently. - Reaching for
evalbecause it appears to work on both formats. - Forgetting that query strings and JSON are different encodings.
- Assuming that successful parsing means the data is semantically valid.
Summary
- Use
json.loadsfor JSON text. - Use
ast.literal_evalfor Python literal dictionary strings. - Use a dedicated parser such as
parse_qsfor query-string input. - Never use
evalfor string-to-dictionary conversion. - Validate keys and types after parsing so the dictionary is actually usable.
Related reading
- String to unique integer hashing
- string.joinlist on object array rather than string array
- Stumped with functional breadth-first tree traversal in Clojure?
- Subgraph enumeration
- Strip HTML from strings in Python
- Stripping everything but alphanumeric chars from a string in Python
- Subtraction over a list of sets
- subtuples for a tuple

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.