What is a JSON API in the context of serving sensor data?
A JSON API is a way to send sensor data from a Raspberry Pi to other devices using a simple text format called JSON. It makes data easy to read and use by programs.
Click to reveal answer
beginner
Why use JSON format to serve sensor data?
JSON is easy to read for both humans and computers. It is lightweight and works well over the internet, making it perfect for sharing sensor data from a Raspberry Pi.
Click to reveal answer
beginner
Which Python library is commonly used on Raspberry Pi to create a simple JSON API?
Flask is a popular Python library used to create web servers that can serve JSON data easily on a Raspberry Pi.
Click to reveal answer
intermediate
How do you convert sensor readings into JSON format in Python?
You can use Python's built-in json module. For example, use json.dumps() to turn a Python dictionary of sensor readings into a JSON string.
Click to reveal answer
beginner
What is the role of an endpoint in a JSON API?
An endpoint is a specific URL on the Raspberry Pi server where sensor data is available. When you visit this URL, you get the sensor data in JSON format.
Click to reveal answer
What does JSON stand for?
AJavaScript Online Network
BJava Standard Output Network
CJavaScript Object Notation
DJava Object Notation
✗ Incorrect
JSON stands for JavaScript Object Notation, a lightweight data format.
Which Python library helps create a web server to serve JSON data on Raspberry Pi?
AMatplotlib
BNumPy
CPandas
DFlask
✗ Incorrect
Flask is a lightweight web framework used to create servers that can serve JSON data.
What is the main purpose of an API endpoint?
ATo provide a URL where data can be accessed
BTo store sensor hardware
CTo convert data to binary
DTo power the Raspberry Pi
✗ Incorrect
An endpoint is a URL where clients can request and receive data.
Which Python function converts a dictionary to a JSON string?
Ajson.load()
Bjson.dumps()
Cjson.parse()
Djson.read()
✗ Incorrect
json.dumps() converts Python objects to JSON strings.
Why is JSON a good choice for sensor data sharing?
AIt is easy to read and lightweight
BIt is heavy and complex
CIt only works on Raspberry Pi
DIt requires special software to read
✗ Incorrect
JSON is easy to read and lightweight, making it ideal for sharing data.
Explain how you would set up a simple JSON API on a Raspberry Pi to serve temperature sensor data.
Think about reading data, converting it, and making it available via a web address.
You got /4 concepts.
Describe the benefits of serving sensor data as a JSON API compared to other methods.
Consider why JSON is popular for data sharing.
You got /4 concepts.
Practice
(1/5)
1.
What is the main purpose of serving sensor data as a JSON API on a Raspberry Pi?
easy
A. To allow other devices to easily access sensor information in a standard format
B. To store sensor data only on the Raspberry Pi without sharing
C. To convert sensor data into images for display
D. To run heavy calculations on sensor data locally
Solution
Step 1: Understand JSON API purpose
A JSON API provides data in a simple, standard format that other devices can read easily.
Step 2: Relate to sensor data sharing
Serving sensor data as JSON means sharing sensor readings so other devices or apps can use them.
Final Answer:
To allow other devices to easily access sensor information in a standard format -> Option A
Quick Check:
JSON API shares sensor data easily [OK]
Hint: JSON API means sharing data in a simple format [OK]
Common Mistakes:
Thinking JSON API stores data only locally
Confusing JSON API with image or video output
Assuming JSON API does heavy local processing
2.
Which of the following is the correct way to import Flask in a Python script on Raspberry Pi?
?
easy
A. import flask
B. import Flask
C. from Flask import flask
D. from flask import Flask
Solution
Step 1: Recall Flask import syntax
The Flask class is imported from the flask module using: from flask import Flask
Step 2: Check options for correct case and syntax
from flask import Flask matches correct syntax and case; others have wrong case or structure.
Flask expects jsonify to create proper response with correct headers, not json.dumps.
Step 2: Identify error cause
Using json.dumps returns a string without correct content-type, causing client issues.
Final Answer:
json.dumps is used instead of jsonify -> Option A
Quick Check:
Use jsonify, not json.dumps, for Flask JSON responses [OK]
Hint: Use jsonify for Flask JSON responses, not json.dumps [OK]
Common Mistakes:
Returning json.dumps output directly in Flask
Not importing jsonify
Assuming json.dumps sets response headers
5.
You want to serve live temperature sensor data from a Raspberry Pi using Flask. Which approach correctly updates the JSON response with real sensor readings each time the API is called?
from flask import Flask, jsonify
app = Flask(__name__)
def read_temp_sensor():
# Returns current temperature reading
return 24.7
@app.route('/temp')
def temp():
# Fill in here
app.run()
hard
A. temp = 24.7
return jsonify({'temperature': temp})
B. temp = read_temp_sensor()
return jsonify({'temperature': temp})
C. return jsonify({'temperature': read_temp_sensor})
D. return {'temperature': read_temp_sensor()} # Flask auto converts
Solution
Step 1: Call sensor function inside route
We must call read_temp_sensor() to get current value each time the route is accessed.
Step 2: Return JSON response with current value
Assign the value to a variable and pass it to jsonify to return proper JSON response.
Final Answer:
temp = read_temp_sensor()
return jsonify({'temperature': temp}) -> Option B
Quick Check:
Call sensor function and jsonify result each request [OK]
Hint: Call sensor function inside route, then jsonify result [OK]
Common Mistakes:
Returning function object instead of calling it
Using fixed value instead of live sensor reading
Returning dict without jsonify in Flask
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.