You are parsing a JSON response that contains a large list of objects. Which technique is most memory-efficient for processing these objects?
Trap 1: Convert the entire JSON to a string then split
Splitting a string still materialises the whole payload plus copies, consuming more memory than the original. It tempts as a way to avoid parsing overhead, but it is the correct choice only for small, already-loaded strings where no streaming parser is available.
Trap 2: Use the 'json.loads()' method on the full response
json.loads() builds the complete Python object graph in memory, so peak usage scales with the entire list. It tempts because it is the standard, convenient API, and would be correct for modest payloads where full random access is needed.
Trap 3: Use a custom regex to parse the JSON
Regex cannot reliably parse JSON's nested structures, escapes and types, and the pattern itself plus intermediate matches add memory. It tempts when only one field is needed, but a streaming parser such as ijson handles that correctly without loading everything.
- A
Convert the entire JSON to a string then split
Why it fails: Splitting a string still materialises the whole payload plus copies, consuming more memory than the original. It tempts as a way to avoid parsing overhead, but it is the correct choice only for small, already-loaded strings where no streaming parser is available.
- B
Use the 'json.loads()' method on the full response
Why it fails: json.loads() builds the complete Python object graph in memory, so peak usage scales with the entire list. It tempts because it is the standard, convenient API, and would be correct for modest payloads where full random access is needed.
- C
Iterate through the list object after parsing
json.loads already materialises the full list, so iterating that parsed object processes elements one at a time without building any additional copy. This avoids duplicating the structure, satisfying the stem's memory-efficiency constraint for large object lists.
- D
Use a custom regex to parse the JSON
Why it fails: Regex cannot reliably parse JSON's nested structures, escapes and types, and the pattern itself plus intermediate matches add memory. It tempts when only one field is needed, but a streaming parser such as ijson handles that correctly without loading everything.