Computer Science Grade 10 20 min

7. Parsing JSON Data

Learn how to parse JSON data received from an API using Python.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define JSON and explain its role in data exchange with APIs. Differentiate between JSON objects and JSON arrays, and identify key-value pairs. Use a programming library (e.g., Python's `json` module) to parse a JSON string into a native data structure. Access and extract specific data points from simple and nested JSON structures. Iterate through a JSON array to process multiple data records. Construct a simple program that fetches data from a mock API and parses the JSON response. Ever wonder how a weather app on your phone gets the latest forecast instantly, or how your favorite social media feed loads new posts? 🤔 The secret is a universal language for data called JSON! In this lesson, you'll learn how to read and understand JSON (JavaScrip...
2

Key Concepts & Vocabulary

TermDefinitionExample JSON (JavaScript Object Notation)A lightweight, text-based format for storing and exchanging data. It's easy for humans to read and write and easy for machines to parse and generate.A simple JSON structure: `{"name": "Alex", "grade": 10, "isActive": true}` JSON ObjectAn unordered collection of key-value pairs, enclosed in curly braces `{}`. It's similar to a dictionary in Python or a map in other languages.`{"studentId": "S123", "major": "Computer Science"}` JSON ArrayAn ordered list of values, enclosed in square brackets `[]`. It's similar to a list or array in most programming languages.`["Databases", "OOP", "Algorithms"]` Key-Value PairThe f...
3

Core Syntax & Patterns

JSON Object Syntax Objects are enclosed in curly braces `{}`. They contain comma-separated key-value pairs. Keys MUST be strings in double quotes. Use this structure to represent a single entity with multiple attributes, like a user profile or a book's details. JSON Array Syntax Arrays are enclosed in square brackets `[]`. They contain comma-separated values. Use this structure to represent an ordered list of items, like a list of comments, search results, or student names. Parsing with a Library In Python: `import json; data = json.loads(json_string)` Always use a dedicated JSON library to parse a JSON string. This function reads the string and converts it into a corresponding data structure (e.g., a Python dictionary for a JSON object, a list for a JSON array)...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
You have parsed the following JSON into a Python variable `data`: `{"user": {"name": "Sam", "roles": ["admin", "editor"]}, "status": "active"}` Which single line of Python code would evaluate to `True`?
A.data['roles'][1] == 'editor'
B.data['user']['roles'] == 'editor'
C.data['user']['roles'][0] == 'editor'
D.'editor' in data['user']['roles']
Challenging
An API returns a JSON array of products, parsed into `products_data`. Each product is an object with a `price` (number) and `inStock` (boolean) key. Which code snippet correctly prints the price of ONLY the products that are in stock?
A.for product in products_data: if product['inStock']: print(product['price'])
B.for product in products_data: if product['price']: print(product['inStock'])
C.for price in products_data['price']: if products_data['inStock']: print(price)
D.if products_data['inStock']: print(products_data['price'])
Challenging
A JSON string `s = '[{"a": 1, "b": {"c": 2}}, {"a": 3, "b": {"c": 4}}]'` is parsed into a Python object `data`. What is the value of the expression `data[1]['b']['c']`?
A.1
B.2
C.4
D.3

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from IV. Interacting with APIs: Data on Demand

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.