Codington
0 of 6 problems solved • 0%

Exercise prompt

Collect JSON Lines

Example

A valid JSON document can contain a scalar instead of an object.

import json for sample in ['{"id": 8}', 'false']: value = json.loads(sample) print(isinstance(value, dict)) # True # False

Decoding successfully tells you the syntax was valid. After decoding, check the shape. A malformed line raises json.JSONDecodeError.

Your task

Implement object_lines(text). Each line may contain one JSON value. Return all decoded objects in order, including empty objects. Skip blank lines, malformed JSON, arrays and scalars. One bad line must not discard the following lines.

object_lines('{"n":1}\noops\n[]\n{}') # [{"n": 1}, {}]