If you want to iterate over both keys and values of the dictionary, do this:

for key, value in data.items():
    print(key, value)
Answer from Lior on Stack Overflow
๐ŸŒ
Python Guides
pythonguides.com โ€บ json-data-in-python
How To Get Values From A JSON Array In Python?
April 27, 2026 - In the real world, JSON is rarely flat. Usually, you have an array inside another object. I recently worked on a project involving US Census data where each state had an array of major cities. To get these values, you have to access the main key first and then loop through the nested array.
Discussions

Accessing data from a json array in python - Stack Overflow
Sign up to request clarification or add additional context in comments. ... Save this answer. ... Show activity on this post. Usually the json will be a string and you will try and deserialise it into a object graph (which in python are typically are made up of maps and arrays). More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to extract from a JSON Array in Python? - Stack Overflow
So, you would retrieve the json ... the get() method to retrieve the value in the dictionary: responseJSON = {...} f = responseJSON [0]['tier'] # value of f: 'PLATINUM' ... json_array = [ { "leagueId":"52dd22c0-0f4a-41e8-8au2-c81f66dacb43", "leagueName":"League1", "tier":"PLATINUM", "queueType":"RANKED_SOLO", "rank":"V", "leaguePoints":0, "wins":131, "losses":117, "hotStreak":False } ] new_list = [dic['tier'] for dic in json_array] ... json_array have invalid python values like ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python get a value in JSON array - Stack Overflow
Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Iโ€™m Jody, the Chief Product and Technology Officer at Stack Overflow. Letโ€™s... 7 Get the value of specific JSON element in Python More on stackoverflow.com
๐ŸŒ stackoverflow.com
September 5, 2019
Python Parse JSON array - Stack Overflow
I'm trying to put together a small python script that can parse out array's out of a large data set. I'm looking to pull a few key:values from each object so that I can play with them later on in the More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
pythontutorials
pythontutorials.net โ€บ blog โ€บ how-to-parse-json-to-get-all-values-of-a-specific-key-within-an-array
How to Parse JSON in Python to Get All Values of a Specific Key Within an Array? โ€” pythontutorials.net
Access the "users" array, then loop through its objects to collect "name" values: # Get the array from the parsed data users_array = data["users"] # Extract all "name" values using a list comprehension names = [user["name"] for user in users_array] ...
๐ŸŒ
Vnservice
test.vnservice.no โ€บ 0sqz6 โ€บ how-to-get-values-from-json-array-in-python
how to get values from json array in python
It is similar to the dictionary in Python. In JSON array, values must be separated by comma. Parse JSON - Convert from JSON to Python If you have a JSON string, you can parse it by using the json.loads() method. For example, if you have a json with the following content โˆ’ Parsing nested JSON ...
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_json.asp
Python JSON
The json.dumps() method has parameters to make it easier to read the result: Use the indent parameter to define the numbers of indents: ... You can also define the separators, default value is (", ", ": "), which means using a comma and a space ...
Top answer
1 of 2
47

In your for loop statement, Each item in json_array is a dictionary and the dictionary does not have a key store_details. So I modified the program a little bit

import json

input_file = open ('stores-small.json')
json_array = json.load(input_file)
store_list = []

for item in json_array:
    store_details = {"name":None, "city":None}
    store_details['name'] = item['name']
    store_details['city'] = item['city']
    store_list.append(store_details)

print(store_list)
2 of 2
1

If you arrived at this question simply looking for a way to read a json file into memory, then use the built-in json module.

with open(file_path, 'r') as f:
    data = json.load(f)

If you have a json string in memory that needs to be parsed, use json.loads() instead:

data = json.loads(my_json_string)

Either way, now data is converted into a Python data structure (list/dictionary) that may be (deeply) nested and you'll need Python methods to manipulate it.


If you arrived here looking for ways to get values under several keys as in the OP, then the question is about looping over a Python data structure. For a not-so-deeply-nested data structure, the most readable (and possibly the fastest) way is a list / dict comprehension. For example, for the requirement in the OP, a list comprehension does the job.

store_list = [{'name': item['name'], 'city': item['city']} for item in json_array]
# [{'name': 'Mall of America', 'city': 'Bloomington'}, {'name': 'Tempe Marketplace', 'city': 'Tempe'}]

Other types of common data manipulation:

  1. For a nested list where each sub-list is a list of items in the json_array.

    store_list = [[item['name'], item['city']] for item in json_array]
    # [['Mall of America', 'Bloomington'], ['Tempe Marketplace', 'Tempe']]
    
  2. For a dictionary of lists where each key-value pair is a category-values in the json_array.

    store_data = {'name': [], 'city': []}
    for item in json_array:
        store_data['name'].append(item['name'])
        store_data['city'].append(item['city'])
    # {'name': ['Mall of America', 'Tempe Marketplace'], 'city': ['Bloomington', 'Tempe']}
    
  3. For a "transposed" nested list where each sub-list is a "category" in json_array.

    store_list = list(store_data.values())
    # [['Mall of America', 'Tempe Marketplace'], ['Bloomington', 'Tempe']]
    
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 57964587 โ€บ getting-values-from-json-array-using-an-array-of-object-and-keys-in-python
Getting values from json array using an array of object and keys in Python - Stack Overflow
September 17, 2019 - ... ... url = urlopen(jsonFile) data = json.loads(url.read()) ... keys = line.split(',') ... # using keys[] to identify the objects and keys value = (data[keys[0]][keys[1]][keys[2]]) ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 68057398 โ€บ looking-to-get-a-particular-value-of-a-json-array-in-python
Looking to get a particular value of a Json array in python - Stack Overflow
Import the json module, parse your json data with json.load (from file) or json.loads (from string). You'll get a python dictionary that you can search. If you need help with that, see the python tutorial.
๐ŸŒ
Zyte
zyte.com โ€บ home โ€บ blog โ€บ json parsing with python [practical guide]
JSON Parsing with Python [Practical Guide]
December 3, 2024 - 1import json 2import jmespath 3 4json_string = '{"numbers": [1, 2, 3], "car": {"model": "Model X", "year": 2022}}' 5json_data = json.loads(json_string) 6 7# Accessing nested JSON 8name = jmespath.search('car.model', json_data) # Result: Model X 9 10# Taking the first number from numbers 11first_number = jmespath.search('numbers[0]', json_data) # Result: 1 ... Those examples only display the basics of what JMESPath can do. JMESPath queries can also filter and transform JSON data. For example, you can use JMESPath to filter a list of objects based on a specific value or to extract specific parts of an object and transform them into a new structure. Let's say we have a JSON array of car objects, each containing information such as the car's make, model, year and price:
Top answer
1 of 3
18

You cannot do contents[:]["name"] since contents is a list is a dictionary with integer indexes, and you cannot access an element from it using a string name.

To fix that, you would want to iterate over the list and get the value for key name for each item

import json
contents = []

try:
    with open("./simple.json", 'r') as f:
        contents = json.load(f)
except Exception as e:
    print(e)


li = [item.get('name') for item in contents]
print(li)

The output will be

['Bulbasaur', 'Ivysaur']
2 of 3
6

This is not a real answer to the question. The real answer is to use a list comprehension. However, you can make a class that allows you to use specifically the syntax you tried in the question. The general idea is to subclass list so that a slice like [:] returns a special view (another class) into the list. This special view will then allow retrieval and assignment from all the dictionaries simultaneously.

class DictView:
    """
    A special class for getting and setting multiple dictionaries
    simultaneously. This class is not meant to be instantiated
    in its own, but rather in response to a slice operation on UniformDictList.
    """
    def __init__(parent, slice):
        self.parent = parent
        self.range = range(*slice.indices(len(parent)))

    def keys(self):
        """
        Retreives a set of all the keys that are shared across all
        indexed dictionaries. This method makes `DictView` appear as
        a genuine mapping type to `dict`.
        """
        key_set = set()
        for k in self.range:
            key_set &= self.parent.keys()
        return key_set

    def __getitem__(self, key):
        """
        Retreives a list of values corresponding to all the indexed
        values for `key` in the parent. Any missing key will raise
        a `KeyError`.
        """
        return [self.parent[k][key] for k in self.range]

    def get(self, key, default=None):
        """
        Retreives a list of values corresponding to all the indexed
        values for `key` in the parent. Any missing key will return
        `default`.
        """
        return [self.parent[k].get(key, default) for k in self.range]

    def __setitem__(self, key, value):
        """
        Set all the values in the indexed dictionaries for `key` to `value`.
        """
        for k in self.range:
            self.parent[k][key] = value

    def update(self, *args, **kwargs):
        """
        Update all the indexed dictionaries in the parent with the specified
        values. Arguments are the same as to `dict.update`.
        """
        for k in self.range:
             self.parent[k].update(*args, **kwargs)


class UniformDictList(list):
    def __getitem__(self, key):
        if isinstance(key, slice):
            return DictView(self, key)
        return super().__getitem__(key)

Your original code would now work out of the box with just one additional wrap in UniformDictList:

import json
try:
    with open("./simple.json", 'r') as f:
        contents = UniformDictList(json.load(f))
except Exception as e:
    print(e)

print(contents[:]["name"])
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-program-to-extract-a-single-value-from-json-response
Python program to extract a single value from JSON response - GeeksforGeeks
July 23, 2025 - Import JSON from the modules. Open the JSON file in read-only mode using the Python with() function. Load the JSON data into a variable using the Python load() function. Now, get the value of keys in a variable.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ loop-through-a-json-array-in-python
Loop through a JSON array in Python - GeeksforGeeks
July 23, 2025 - In this example, we will define the JSON data as a string and load it using the and the load() function to convert the JSON data to a Python object. Then using a for loop we will iterate through the array.
๐ŸŒ
DevQA
devqa.io โ€บ python-parse-json
How to Parse JSON in Python
Now that we have our JSON as a Python dictionary, we can fetch certain data by specifying the field, which represents the key in the dictionary. For example, to fetch the price of the bicycle in the above JSON, we would use: ... In the above ...
๐ŸŒ
DEV Community
dev.to โ€บ bluepaperbirds โ€บ get-all-keys-and-values-from-json-object-in-python-1b2d
Get all keys and values from json object in Python - DEV Community
January 12, 2021 - Here we use a for loop to iterate over the keys and values. If your json file is small like the one in this example, you don't necessarily have to use a loop.