multiple JSON documents in one file, change proposal

Cameron Simpson cs at cskk.id.au
Fri Nov 30 17:50:12 EST 2018


On 01Dec2018 00:40, Marko Rauhamaa <marko at pacujo.net> wrote:
>Paul Rubin <no.email at nospam.invalid>:
>> Maybe someone can convince me I'm misusing JSON but I often want to
>> write out a file containing multiple records, and it's convenient to
>> use JSON to represent the record data.
>>
>> The obvious way to read a JSON doc from a file is with "json.load(f)"
>> where f is a file handle. Unfortunately, this throws an exception
>
>I have this "multi-JSON" need quite often. In particular, I exchange
>JSON-encoded messages over byte stream connections. There are many ways
>of doing it. Having rejected different options (<URL:
>https://en.wikipedia.org/wiki/JSON_streaming>), I settled with
>terminating each JSON value with an ASCII NUL character, which is
>illegal in JSON proper.

There's a common format called Newline Delimited JSON (NDJSON) for just 
this need.

Just format the outbound records as JSON with no newlines (i.e. make the 
separator a space or the empty string), put a newline at the end of 
each.

On ingest, read lines of text, and JSON parse each line separately.

This is very easy, very partable, and makes for very human readable data 
files.

I strongly recommend it.

Cheers,
Cameron Simpson <cs at cskk.id.au>



More information about the Python-list mailing list