Parse JSON Lines with useful line numbers
JSON Lines stores one JSON value on each line, making it convenient for incremental processing. Each line is decoded separately; concatenating all the lines and passing them to one json.loads call describes a different format.
Preserve physical line numbers while skipping blank lines so an error points to the source a person can inspect. A valid JSON scalar may still violate an application's record schema, so decoding and shape validation are separate steps. This exercise accepts only object records and returns them in file order, with no network or pre-existing file dependency.
import json
text = '{"id":1}\n\n{"id":2}\n'
records = [json.loads(line)
for line in text.splitlines()
if line.strip()]
print(records)Your task
Implement json_records(text): decode each nonblank line as a JSON object, preserving order. Raise ValueError mentioning the physical line number on malformed JSON or a nonobject value.
The explanation opens here once every check passes.