Working with JSON (JavaScript Object Notation) is a common requirement when dealing with RESTful APIs, data storage, or even configuration files.
Python includes a built-in package called json that allows working with JSON data in a straightforward and efficient way. In this tutorial, we will cover how to get a specific value from a JSON object in Python using various methods.
Step 1: Read JSON Data
First, let’s create a sample JSON object for illustration purposes. You can replace it with your own JSON object either by loading from a file or fetching from an API.
1 2 3 4 5 6 7 8 9 10 11 12 |
json_data = ''' { "name": "John", "age": 30, "city": "New York", "contacts": { "email": "[email protected]", "phone": "555-123-456" }, "hobbies": ["reading", "gaming", "hiking"] } ''' |
Now we need to parse this JSON data into a Python dictionary. We will use the loads() function from the json package to accomplish this. If you are reading the JSON data from a file, you can use the load() function instead.
1 2 3 4 |
import json # Parse JSON data into a Python dictionary json_object = json.loads(json_data) |
Step 2: Access JSON Object Value Directly
If you know the path to the specific value in your JSON object, you can access it directly using the key of the dictionary. For example, to access the “name” and “city” of the person in the JSON object, you can simply do:
1 2 3 4 5 |
name = json_object["name"] city = json_object["city"] print("Name:", name) print("City:", city) |
This would display:
Name: John City: New York
Step 3: Access Nested JSON Object Values
To access nested JSON object values, you can chain the keys using the dictionary indexing. For example, to access “email” and “phone” from the “contacts” object, you can do:
1 2 3 4 5 |
email = json_object["contacts"]["email"] phone = json_object["contacts"]["phone"] print("Email:", email) print("Phone:", phone) |
This would display:
Email: [email protected] Phone: 555-123-456
Step 4: Access JSON Array Values
To access values from a JSON array, simply use the index of the element. For example, to access the first and last hobbies in the “hobbies” array, you can do:
1 2 3 4 5 |
first_hobby = json_object["hobbies"][0] last_hobby = json_object["hobbies"][-1] print("First hobby:", first_hobby) print("Last hobby:", last_hobby) |
This would display:
First hobby: reading Last hobby: hiking
Full Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import json json_data = ''' { "name": "John", "age": 30, "city": "New York", "contacts": { "email": "[email protected]", "phone": "555-123-456" }, "hobbies": ["reading", "gaming", "hiking"] } ''' json_object = json.loads(json_data) name = json_object["name"] city = json_object["city"] print("Name:", name) print("City:", city) email = json_object["contacts"]["email"] phone = json_object["contacts"]["phone"] print("Email:", email) print("Phone:", phone) first_hobby = json_object["hobbies"][0] last_hobby = json_object["hobbies"][-1] print("First hobby:", first_hobby) print("Last hobby:", last_hobby) |
Output:
Name: John City: New York Email: [email protected] Phone: 555-123-456 First hobby: reading Last hobby: hiking
Conclusion
In this tutorial, we learned how to get specific values from a JSON object in Python using the built-in json package. We can now access values directly, nested values, and even values within arrays. This skill is essential when working with JSON data from APIs or other sources.