Use json.loads() to decode a JSON string or json.load() to read a JSON file. Then find the list of records and compare the field you want, such as item.get("status") == "open". Check which records are dictionaries and whether a missing or null status should be skipped or reported.
Find the list and check its records
A JSON document may contain a list at the top level or an object holding records under a field such as items. Before iterating, read the source contract. Iterating a dictionary yields keys. Iterating a string yields characters. Both loops run successfully, even when the code should have been processing records.
import json
raw = '[{"id":"A","status":"open"},{"id":"B","status":null},{"id":"C"}]'
records = json.loads(raw)
if not isinstance(records, list):
raise ValueError("Expected a list of records")
selected = []
needs_review = []
for index, item in enumerate(records, start=1):
if not isinstance(item, dict) or not isinstance(item.get("status"), str):
needs_review.append(index)
continue
if item["status"] == "open":
selected.append(item)
assert [item["id"] for item in selected] == ["A"]
assert needs_review == [2, 3]
The example treats both null and absent statuses as review cases because the filter requires a status string. In another system they can mean different things. Check "status" in item to distinguish a field that exists with value None from a field that was omitted. Avoid a broad truth test for a numeric field if zero is valid.
Count matches, nonmatches, and rejected records
Filtering can mean “remove all statuses other than open” or “return the open ones and account for unreadable records.” Those deliverables differ. If a report has operational consequences, record how many entries passed, how many were valid and outside the filter, and how many could not be classified. The example keeps the input position of invalid entries. A report can store problem reasons and a stable source identifier when available.
Preserve the order if it matters to the consumer. JSON arrays have an ordering, but a record embedded in an object mapping needs a separate documented ordering rule if the report depends on one. If you sort by date or ID, parse the fields according to their expected types. Sorting numeric identifiers as strings gives a different order from sorting numbers.
Handle nested files and large inputs
Some payloads wrap the record list and include an export period or pagination fields. Preserve this metadata when creating a filtered output. If a source contains multiple pages, filtering only the first page may yield a valid-looking but incomplete result. Use a small test payload with two pages or wrappers if those are part of the contract.
The standard library’s usual json.load() API decodes one complete JSON document. For very large files, consider JSON Lines when the format is under your control. In that format, each line carries its own complete JSON value, so a program can parse and classify one record at a time. Arbitrarily reading line by line through a formatted JSON array does not have the same behavior. Put limits on the size of untrusted input before parsing it.
Write a separate result and test the rejected cases
Use json.dump() to write the selected records to a separate file. If a consumer expects a bare JSON array, preserve that shape and record rejection counts separately. If the output can use an object to wrap the list, give it explicit fields such as matched, not_matched and needs_review. See the CSV and JSON guide for other format checks.
Verify inputs with the wrong top-level shape, a nonobject record, a missing field, a null field, and an output with no matches. A legitimate empty result can be useful. It should be distinguishable from an interruption or malformed source. Keep the filter criteria visible so a reader knows how to reproduce the selection.