Extract Data from Nested API JSON
Advanced · Write the code · from Python JSON: Parse, Create, and Manipulate JSON Data
From the lesson: Python JSON: Parse, Create, and Manipulate JSON Data
Imagine a universal language that every app, website, and database can understand. That's JSON (JavaScript Object Notation). It's the most popular format for sending data between computers.
Read the lessonYou are given a JSON string representing an API response with a list of users. Parse it and print each user's name and their first hobby.
The JSON data:
{"users": [{"name": "Alice", "hobbies": ["chess", "piano"]}, {"name": "Bob", "hobbies": ["soccer", "cooking"]}, {"name": "Charlie", "hobbies": ["painting", "yoga"]}]}Expected output:
Alice likes chess
Bob likes soccer
Charlie likes paintingimport json
json_str = '{"users": [{"name": "Alice", "hobbies": ["chess", "piano"]}, {"name": "Bob", "hobbies": ["soccer", "cooking"]}, {"name": "Charlie", "hobbies": ["painting", "yoga"]}]}'
# Your code here
More JSON exercises
- Parse a JSON StringBeginner
- Predict the JSON OutputBeginner
- Create Pretty JSONIntermediate
- Fix the JSON Parsing BugIntermediate
- Refactor String Concatenation to json.dumpsIntermediate
All Python exercises · 50 practice problems · File I/O & Data Formats quiz