How to get values from json array in Python

In this Python tutorial, I will show you, how to get values from JSON array in Python. Also, I will explain, how to extract specific data from JSON in Python. Finally, I will show you how to read a JSON file in Python.

JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. It’s widely used to store and exchange data in web applications.

Introduction to JSON in Python

Python has a built-in module called json which can be used to work with JSON data. JSON data structures consist of arrays and objects. In Python, JSON arrays are equivalent to lists and JSON objects are equivalent to dictionaries.

Here’s an example of JSON data representing cities like below:

{
    "cities": [
        {
            "name": "New York",
            "state": "NY",
            "population": 8419600
        },
        {
            "name": "Los Angeles",
            "state": "CA",
            "population": 3990456
        },
        {
            "name": "Chicago",
            "state": "IL",
            "population": 2718500
        }
    ]
}

Read JSON File in Python

Let us see, how to read a JSON file in Python.

To read JSON data from a file, you can use the built-in open() function to open the file and then use the json.load() function to parse the JSON data in Python.

First, let’s create a file named cities.json with the above JSON data.

Now, let’s write a Python code to read this data:

import json

# Open the JSON file for reading
with open('cities.json', 'r') as file:
    # Parse JSON data
    data = json.load(file)

# Display the data
print(data)

How to Parse JSON Data in Python

Let us now check out how to parse JSON data in Python.

READ:  Python File methods (With Useful Examples)

The json module also allows you to parse JSON data from a string using the json.loads() function.

Example:

import json

json_string = '{"name": "Boston", "state": "MA", "population": 685094}'

# Parse JSON data from a string
city = json.loads(json_string)

# Display the data
print(city)

You can see the output below:

how to get values from json array in python

Python extract value from JSON array

Let us now see, how to extract value from JSON array in Python.

Let’s extract values from the JSON array. The JSON array here contains objects, and each object represents a city.

# Using the data loaded earlier from the file
cities = data['cities']

# Loop through the array and print each city name
for city in cities:
    print(city['name'])

Output:

New York
Los Angeles
Chicago

Extract specific data from JSON in Python

Now. let us see, how to extract specific data from JSON in Python.

Sometimes you might only need specific data from the JSON. Let’s say you only want the names of the cities that have a population of over 3 million.

# Loop through the array and print names of cities with population > 3 million
for city in cities:
    if city['population'] > 3000000:
        print(city['name'])

Output:

New York
Los Angeles

Conclusion

In this tutorial, we have learned how to read JSON data from a file and parse JSON data in Python. We have also learned how to extract values and specific data from JSON arrays. The json module in Python makes it easy to work with JSON data, allowing for the encoding and decoding of JSON to Python data structures like lists and dictionaries.

You may also like: