JSON Lines

How do you save tabular data?

CSV comes to mind, but CSV can be too simplistic for the data to be saved. And CSV can have different column separators and different methods for character escaping. Too confusing. Thanks, but no thanks.

A JSON array to the rescue? Maybe, but why spreading each row across multiple lines? (If proper JSON formatting is applied.)

Recently, I stumbled across a data format that looks as if CSV and JSON had a child:

JSON Lines.

JSON Lines, or JSONL, is a line-oriented format. It is not JSON, but each line is valid JSON. This is a clever idea. A JSONL file can be processed line by line. Unix CLI tools love this! Imagine running grep, awk, wc or other Unix-ish tools on your “JSON” data. And each line can contain data that is more complex than a line of CSV, with much less ambiguity.

Bonus: encoding/json’s Encoder is able to write JSON lines out of the box. Every call to Encode() generates a single line of JSON with a newline at the end.

type line struct {
	A string
	B int
}

func main() {
	e := json.NewEncoder(os.Stdout)
	e.Encode([]line{
		{"Area", 51},
		{"Route", 66},
	})
	e.Encode([]line{
		{"Heaven", 17},
		{"Catch", 22},
	})
}

Output:

[{"A":"Area","B":51},{"A":"Route","B":66}]
[{"A":"Heaven","B":17},{"A":"Catch","B":22}]

(Playground)

There you have it: readable, CLI-tool-friendly tabular data with some “extra structure” in each column.