You are reading a page on Ben Weaver's Website.

Decode multiple JSON objects from one file

2010.04.01 Read More python json

Python's json.loads only decodes one JSON object. This example shows how raw_decode can be used to load multiple JSON values from the same buffer.

I'm writing a program to load external Avro schemas from a file, but the json.load method raises a ValueError if there's any non-whitespace data after the first JSON object. Using this procedure instead allows multiple schema declarations in the same file to be processed successfully.

def iload_json(buff, decoder=None, _w=json.decoder.WHITESPACE.match):
    """Generate a sequence of top-level JSON values declared in the

    >>> list(iload_json('[1, 2] "a" { "c": 3 }'))
    [[1, 2], u'a', {u'c': 3}]

    decoder = decoder or json._default_decoder
    idx = _w(buff, 0).end()
    end = len(buff)

        while idx != end:
            (val, idx) = decoder.raw_decode(buff, idx=idx)
            yield val
            idx = _w(buff, idx).end()
    except ValueError as exc:
        raise ValueError('%s (%r at position %d).' % (exc, buff[idx:], idx))