JSON as standard format in Rest API - Time & Space Complexity
When working with REST APIs, JSON is the common way to send and receive data. Understanding how processing JSON scales helps us build faster apps.
We want to know how the time to handle JSON changes as the data size grows.
Analyze the time complexity of the following code snippet.
// Example: Parsing JSON data from an API response
const jsonData = '{"users": [{"id": 1}, {"id": 2}, {"id": 3}]}';
const data = JSON.parse(jsonData);
for (const user of data.users) {
console.log(user.id);
}
This code parses a JSON string and loops through the users to print their IDs.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array of users after parsing JSON.
- How many times: Once for each user in the array.
As the number of users grows, the time to parse and loop grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 loops plus parsing |
| 100 | About 100 loops plus parsing |
| 1000 | About 1000 loops plus parsing |
Pattern observation: The time grows roughly in direct proportion to the number of users.
Time Complexity: O(n)
This means the time to process the JSON grows linearly with the number of items inside it.
[X] Wrong: "Parsing JSON is instant and does not depend on data size."
[OK] Correct: Parsing reads every character, so bigger JSON takes more time to process.
Knowing how JSON parsing and data traversal scale helps you explain API performance clearly and confidently.
"What if we used nested JSON objects instead of a flat array? How would the time complexity change?"
Practice
Solution
Step 1: Understand JSON format
JSON is a text-based format that uses simple syntax with objects and arrays.Step 2: Identify why REST APIs prefer JSON
REST APIs use JSON because it is easy for humans to read and easy for machines to parse.Final Answer:
Because JSON is easy to read and write for both humans and machines -> Option AQuick Check:
JSON is text-based and readable [OK]
- Thinking JSON is binary
- Believing JSON needs special software
- Assuming JSON only supports numbers
Solution
Step 1: Recall JSON object syntax
JSON objects use curly braces with keys and values as strings, keys must be in double quotes.Step 2: Check each option
{"name": "Alice"} uses double quotes correctly around key and value, with colon separator.Final Answer:
{"name": "Alice"} -> Option DQuick Check:
Keys and strings in double quotes [OK]
- Omitting quotes around keys
- Using square brackets for objects
- Using equals sign instead of colon
{"status": "success", "data": [10, 20, 30]}What is the value of the key "data"?
Solution
Step 1: Identify the key "data" in JSON
The key "data" holds an array with values 10, 20, and 30.Step 2: Match the value to the options
[10, 20, 30] correctly shows the array [10, 20, 30].Final Answer:
[10, 20, 30] -> Option BQuick Check:
Key "data" = array [10, 20, 30] [OK]
- Confusing key "status" value with "data"
- Picking a single number instead of the array
- Choosing the whole JSON object as value
{"user": "Bob", "age": 25,}What is wrong with this JSON?
Solution
Step 1: Check JSON syntax rules
JSON objects cannot have a comma after the last key-value pair.Step 2: Identify the error in the string
The comma after "age": 25 is invalid and causes a syntax error.Final Answer:
Trailing comma after the last item is invalid -> Option AQuick Check:
No trailing commas allowed in JSON objects [OK]
- Thinking keys must be numbers
- Believing numbers are invalid values
- Adding brackets incorrectly
Solution
Step 1: Check correct JSON types for each field
Name is a string, age is a number, hobbies is an array of strings.Step 2: Validate each option's syntax
{"name": "Eve", "age": 28, "hobbies": ["reading", "swimming"]} correctly uses double quotes for strings, numbers without quotes, and square brackets for array.Final Answer:
{"name": "Eve", "age": 28, "hobbies": ["reading", "swimming"]} -> Option CQuick Check:
Strings in quotes, numbers plain, arrays in brackets [OK]
- Using parentheses instead of brackets for arrays
- Not quoting string values
- Putting numbers in quotes unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
