JSON payload formatting in IOT Protocols - Time & Space Complexity
When working with IoT devices, sending data as JSON payloads is common. It's important to understand how the time to format these payloads changes as the data grows.
We want to know: How does the time to create a JSON message grow when we add more data items?
Analyze the time complexity of the following code snippet.
function formatPayload(dataList) {
let payload = '{"sensors": [';
for (let i = 0; i < dataList.length; i++) {
payload += '{"id": "' + dataList[i].id + '", "value": ' + dataList[i].value + '}';
if (i < dataList.length - 1) payload += ', ';
}
payload += ']}';
return payload;
}
This code builds a JSON string from a list of sensor data objects.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that goes through each sensor data item.
- How many times: Exactly once for each item in the dataList array.
As the number of sensor data items increases, the loop runs more times, adding more text to the payload.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 string concatenations |
| 100 | About 100 string concatenations |
| 1000 | About 1000 string concatenations |
Pattern observation: The work grows directly with the number of items; doubling items roughly doubles the work.
Time Complexity: O(n)
This means the time to format the JSON grows in a straight line with the number of data items.
[X] Wrong: "Adding more data items won't affect formatting time much because it's just text."
[OK] Correct: Each item adds work because the code loops through all items and builds the string piece by piece, so more items mean more time.
Understanding how data size affects processing time is a key skill. It shows you can predict how your code behaves as things scale, which is important in real IoT systems.
"What if we used a method that builds the JSON object first and then converts it to a string at once? How would the time complexity change?"
Practice
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 AQuick Check:
JSON = clear data format [OK]
- Confusing JSON formatting with encryption
- Thinking JSON compresses data
- Assuming JSON converts data to binary
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 BQuick Check:
Keys need double quotes in JSON [OK]
- Using single quotes instead of double quotes
- Omitting quotes around keys
- Using equals sign instead of colon
{"device": "sensor1", "status": "active", "battery": 85}What is the value of the
battery key?Solution
Step 1: Locate the battery key in JSON
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 AQuick Check:
Number values have no quotes in JSON [OK]
- Confusing numbers with strings by adding quotes
- Selecting the key name instead of its value
- Assuming missing keys return null
{"temp": 25, "humidity": 55,}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 DQuick Check:
No trailing commas allowed in JSON [OK]
- Leaving trailing commas after last pair
- Forgetting quotes around keys
- Mixing single and double quotes
Solution
Step 1: Understand nested JSON objects
Nested objects use curly braces { } with key-value pairs inside, so location must be an object with lat and lon keys.Step 2: Check each option's structure
{ "temperature": 20, "status": "ok", "location": { "lat": 40.7128, "lon": -74.0060 } } uses curly braces correctly. { "temperature": 20, "status": "ok", "location": [ "lat": 40.7128, "lon": -74.0060 ] } uses square brackets which are for arrays, not objects. { "temperature": 20, "status": "ok", "location": ( "lat": 40.7128, "lon": -74.0060 ) } uses parentheses which are invalid. { "temperature": 20, "status": "ok", "location": "lat": 40.7128, "lon": -74.0060 } has keys outside an object.Final Answer:
{ "temperature": 20, "status": "ok", "location": { "lat": 40.7128, "lon": -74.0060 } } -> Option CQuick Check:
Nested objects use curly braces { } [OK]
- Using brackets [] instead of braces {} for objects
- Using parentheses () which are invalid in JSON
- Placing keys outside any object or array
Ready to prove it?
Pass the test and this pattern moves unknown
