Newline delimited JSON is awesome

The power of JSON without reading a whole JSON.

Jaga Santagostino
2 min readJan 20, 2018

Whats wrong with JSON?

Nothing, JSON is a great format, it is the de-facto standard for data comunication and is supported everywhere.

JSON on the left, newline-delimited JSON (aka ndjson) on the right

So what is ndJSON?

ndJSON is a collection of JSON objects, separated by `\n`

So it’s one valid JSON object per line

Ok but why?

Personally i had a use case where I needed to:

  • Store some logs
  • I wanted the log entries to be JSON
  • I wanted quick insertions
  • I needed to query the last or last (n) items quickly

The problems

Since i wanted to store JSON a JSON-like database like MongoDB was the obvious choise, but i wanted to keep things very simple, I just wanted to store the logs in a text file.

So using a json file feeld obvious right?

Yes, but…

This would mean that in order to insert or read a record from the file i had to parse the whole file, which is far from ideal

ndJSON to the rescue

Since every entry is valid json it can be parsed/unmarshaled as a standlone JSON doument, no need do read the file in memory before parse

This makes the file streamable, knowing that every new line means a new entry i can read just as much lines i need to get the same amout of records

More info at http://ndjson.org

--

--

Jaga Santagostino

Independent Software consultant lavoro.devmilano.dev @reactjs_milano organizer — photographer