Converting JSON String to Dictionary Not List
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
In Python, json.loads() does not guess whether you want a dictionary or a list. It follows the structure of the JSON text. If the top-level JSON value is an object, you get a dict. If the top-level value is an array, you get a list.
The Top-Level JSON Shape Decides the Python Type
A JSON object becomes a dictionary:
The parsed type is dict because the JSON starts with an object.
A JSON array becomes a list:
This time the parsed type is list because the top-level JSON value is an array.
Validate the Root Type If You Need a Dictionary
If your program requires a dictionary, parse first and then validate that the root value has the expected type.
This is the cleanest way to fail early when an API or file gives you a shape your code does not support.
Turning a List into a Dictionary Requires a Rule
If the incoming JSON is a list but your code wants a dictionary, the parser is not wrong. The data shape is different from your desired application shape. You need a second transformation step.
For example, you might convert a list of objects into a dictionary keyed by id:
That dictionary is not the direct parse result. It is a deliberate transformation built on top of the parsed list.
object_hook Does Not Change Array Roots
Python's JSON decoder supports hooks such as object_hook, but those hooks run when JSON objects are decoded. If the top-level value is an array, the outermost result is still a list.
That means hooks can customize inner dictionaries, but they do not magically convert a list root into a dictionary root. If the JSON starts with [ and ], the top-level parsed container is still list-shaped.
Watch for Invalid JSON Syntax
Another common source of confusion is mixing Python literal syntax with JSON syntax. JSON requires double quotes around keys and string values.
Valid JSON:
Invalid JSON:
If the input uses Python-style single quotes, json.loads() fails before type conversion even becomes relevant.
Common Pitfalls
Expecting json.loads() to return a dictionary when the top-level JSON value is actually an array is the core misunderstanding behind this issue.
Trying to force a list into a dictionary without a clear transformation rule usually creates brittle code later.
Confusing Python literal syntax with JSON syntax leads to decoding errors that can distract from the real type-shape question.
Assuming every API response should be a dictionary is incorrect because many APIs are designed to return top-level lists.
Validating the type too late makes downstream code fail in less obvious places than an early explicit check would.
Summary
- '
json.loads()returns adictfor a JSON object and alistfor a JSON array.' - If your code requires a dictionary, validate the parsed root type explicitly.
- Converting a parsed list into a dictionary requires a second transformation rule.
- '
object_hookcustomizes decoded objects but does not turn a top-level array into a dictionary.' - Keep JSON syntax and Python literal syntax separate when debugging parse results.
Related reading
- Converting Python dict to kwargs?
- Copy array items into another array
- Copy variables from one TensorFlow graph to another
- Cosine Similarity between 2 Number Lists
- Converting LinearSVC's decision function to probabilities Scikit learn python
- converting list of header and row lists into pandas DataFrame
- Could not reserve enough space for object heap
- count distinct slices in an array

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.