An NDJSON (Newline Delimited JSON) / JSON Lines parser and serializer implemented as an mq module.
- Parse NDJSON strings into arrays of mq values
- Serialize arrays of mq values to NDJSON strings
- Parse individual NDJSON lines
- Ignore empty and whitespace-only lines
- Handle strings, numbers, booleans, null, arrays, and objects
- No external dependencies (uses mq's built-in JSON parser)
Copy ndjson.mq to your mq module directory:
cp ndjson.mq ~/.mq/Or reference it directly with -L:
mq -L /path/to/ndjson.mq 'import "ndjson" | ...' file.ndjsonmq -I raw 'import "ndjson" | ndjson::ndjson_parse(.) | map(fn(x): x["id"];)' data.ndjsonParses an NDJSON string and returns an array of mq values. Empty and whitespace-only lines are ignored.
Parses a single NDJSON line into a mq value. Leading and trailing whitespace is trimmed.
Serializes an array of mq values to an NDJSON string (one JSON value per line). If a single non-array value is given, it is wrapped in an array first.
Given events.ndjson:
{"id":1,"event":"login","user":"alice"}
{"id":2,"event":"purchase","user":"bob","amount":49.99}
{"id":3,"event":"logout","user":"alice"}
# Extract all user names
mq -I raw 'import "ndjson" | ndjson::ndjson_parse(.) | map(fn(x): x["user"];)' events.ndjson
# => ["alice", "bob", "alice"]
# Filter by event type
mq -I raw 'import "ndjson" | ndjson::ndjson_parse(.) | filter(fn(x): x["event"] == "login";)' events.ndjson
# => [{"id":1,"event":"login","user":"alice"}]
# Re-serialize after transformation
mq -I raw 'import "ndjson" | ndjson::ndjson_stringify(ndjson::ndjson_parse(.))' events.ndjsonRequires mq v0.5 or later.
MIT