[issue34529] add the option for json.dumps to return newline delimited json

Bob Ippolito report at bugs.python.org
Wed Aug 29 19:28:53 EDT 2018


Bob Ippolito <bob at redivi.com> added the comment:

I think the best start would be to add a bit of documentation with an example of how you could work with newline delimited json using the existing module as-is. On the encoding side you need to ensure that it's a compact representation without embedded newlines, e.g.:

    for obj in objs:
        yield json.dumps(obj, separators=(',', ':')) + '\n'

I don't think it would make sense to support this directly from dumps, as it's really multiple documents rather than the single document that every other form of dumps will output.

On the read side it would be something like:

    for doc in lines:
        yield json.loads(doc)

I'm not sure if this is common enough (and separable enough from I/O and error handling constraints) to be worth adding the functionality directly into json module. I think it would be more appropriate in the short to medium term that the each service (e.g. BigQuery) would have its own module with helper functions or framework that encapsulates the protocol that the particular service speaks.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34529>
_______________________________________


More information about the Python-bugs-list mailing list