📖 Scenario: You are working with IoT devices that send sensor data to a cloud server. The data must be formatted as a JSON payload before sending.
🎯 Goal: Build a correctly formatted JSON payload containing sensor readings and device information.
📋 What You'll Learn
Create a dictionary with device and sensor data
Add a configuration variable for temperature unit
Format the sensor data into a JSON payload string
Print the final JSON payload
💡 Why This Matters
🌍 Real World
IoT devices send sensor data as JSON payloads to cloud servers for monitoring and analysis.
💼 Career
Understanding JSON formatting is essential for DevOps engineers working with IoT data pipelines and cloud integrations.
Progress0 / 4 steps
1
Create the initial sensor data dictionary
Create a dictionary called sensor_data with these exact entries: "device_id": "sensor_001", "temperature": 22.5, "humidity": 45
IOT Protocols
Hint
Use curly braces to create a dictionary with keys and values exactly as shown.
2
Add a temperature unit configuration
Create a variable called temp_unit and set it to the string "Celsius"
IOT Protocols
Hint
Assign the string "Celsius" to the variable temp_unit.
3
Format the JSON payload string
Import the json module and create a variable called json_payload that converts sensor_data to a JSON string using json.dumps(). Then add the temp_unit to the dictionary before conversion.
IOT Protocols
Hint
Use import json at the top. Add the temp_unit key to sensor_data, then use json.dumps(sensor_data) to create the JSON string.
4
Print the JSON payload
Write a print() statement to display the json_payload variable
IOT Protocols
Hint
Use print(json_payload) to display the JSON string.
Practice
(1/5)
1. What is the main purpose of using JSON payloads in IoT communication?
easy
A. To organize data in a clear, readable way
B. To encrypt data for security
C. To compress data for faster transfer
D. To convert data into binary format
Solution
Step 1: Understand JSON payload role
JSON payloads structure data using key-value pairs, making it easy to read and understand.
Step 2: Compare with other options
Encryption, compression, and binary conversion are different processes not handled by JSON formatting.
Final Answer:
To organize data in a clear, readable way -> Option A
Quick Check:
JSON = clear data format [OK]
Hint: JSON is for clear data structure, not encryption or compression [OK]
Common Mistakes:
Confusing JSON formatting with encryption
Thinking JSON compresses data
Assuming JSON converts data to binary
2. Which of the following is a correctly formatted JSON payload snippet?
easy
A. { 'temperature': 22, 'humidity': 60 }
B. { "temperature": 22, "humidity": 60 }
C. { temperature: 22, humidity: 60 }
D. { temperature = 22, humidity = 60 }
Solution
Step 1: Check JSON key formatting
JSON keys must be in double quotes, so "temperature" and "humidity" are correct.
Step 2: Validate value syntax
Values like 22 and 60 are valid numbers without quotes. Other options use invalid quotes or syntax.
Final Answer:
{ "temperature": 22, "humidity": 60 } -> Option B
Quick Check:
Keys need double quotes in JSON [OK]
Hint: Keys in JSON always use double quotes " " [OK]
The key "battery" has the value 85 without quotes, meaning it is a number.
Step 2: Understand JSON value types
Numbers in JSON are written without quotes, so the value is the number 85, not a string.
Final Answer:
85 -> Option A
Quick Check:
Number values have no quotes in JSON [OK]
Hint: Numbers in JSON have no quotes, strings do [OK]
Common Mistakes:
Confusing numbers with strings by adding quotes
Selecting the key name instead of its value
Assuming missing keys return null
4. Identify the error in this JSON payload:
{"temp": 25, "humidity": 55,}
medium
A. Missing quotes around keys
B. Keys and values swapped
C. Using single quotes instead of double quotes
D. Trailing comma after last item
Solution
Step 1: Check JSON syntax for commas
JSON does not allow a comma after the last key-value pair, so the comma after 55 is invalid.
Step 2: Verify other syntax elements
Keys have double quotes, and values are correct. No other syntax errors present.
Final Answer:
Trailing comma after last item -> Option D
Quick Check:
No trailing commas allowed in JSON [OK]
Hint: No comma after last item in JSON objects [OK]
Common Mistakes:
Leaving trailing commas after last pair
Forgetting quotes around keys
Mixing single and double quotes
5. You want to send a JSON payload from an IoT device reporting temperature and status. Which of these payloads correctly formats a nested JSON object for location with latitude and longitude?