You are here because when you decoded a JSON data and expected JSON data to be a dict
type, but it comes out as a list
type.
Further Reading:
- Solve Python JSON Exercise to practice Python JSON skills
In other words, You want to parse a JSON file and convert the JSON data into a dictionary so you can access JSON using its key-value pairs, but when you parse JSON data, you received a list, not a dictionary. In this article, we will see how to access data in such situations. Before that, first, understand why this happens.
- It can happen because your JSON is an array with a single object inside, for example, somebody serialized the Python list into JSON. So when you parse it, you get a list object in return. In this case, you need to iterate the list to access data.
- Also, if somebody serialized Python list(which contains a dictionary) into JSON. When you parse it, you will get a list with a dictionary inside. We will see how to access such data.
We will see both examples. but first, understand the scenario with an example.
import json
sampleList = [125, 23, 85, 445]
# Serialization
print("serialize into JSON and write into a file")
with open("sampleFile.json", "w") as write_file:
json.dump(sampleList, write_file)
print("Done Writing into a file")
# Deserialization
print("Started Reading JSON data from file")
with open("sampleFile.json", "r") as read_file:
data = json.load(read_file)
print("Type of deserialized data: ", type(data))
Output:
serialize into JSON and write into a file Done Writing into a file Started Reading JSON data from file Type of deserialized data: <class 'list'>
As you can see, we received a list from the json.load() method because we serialized only list type object. Now we can access data by iterating it like this. just add the following lines at the end of the above example and execute it.
print("Data is")
for i in data:
print(i)
Output:
Data is 125 23 85 445
Deserialize a JSON array that contains a dictionary inside
Now, Let’s see the second scenario. Let’s assume somebody serialized Python list(which contains a dictionary) into JSON. i.e., The list contains a dictionary inside.
In this example, I am serializing the following MarksList into JSON.
StudentDict = {"id": 22, "name": "Emma"}
MarksList = [StudentDict, 78, 56, 85, 67]
You can access the actual dictionary directly by accessing 0th items from the list. Let’s see the example now.
import json
StudentDict = {"id": 22, "name": "Emma"}
MarksList = [StudentDict, 78, 56, 85, 67]
# Serialization
encodedJson = json.dumps(MarksList, indent=4)
# Deserialization
data = json.loads(encodedJson) # or you can read from file using load()
print("Type of deserialized data: ", type(data))
print("JSON Data is")
for i in data:
if isinstance(i, dict):
for key, value in i.items():
print(key, value)
else:
print(i)
Output:
Type of deserialized data: <class 'list'> JSON Data is id 22 name Emma 78 56 85 67
Also, you can access limited data directly using a key name using the following code.
studentId = data[0]["id"]
studentName = data[0]["name"]
print(studentId, studentName)
Let’ I know if this doesn’t answer your question in the comment section.