Skip to content

harehare/ndjson.mq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

ndjson.mq

An NDJSON (Newline Delimited JSON) / JSON Lines parser and serializer implemented as an mq module.

Features

  • 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)

Installation

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.ndjson

Usage

mq -I raw 'import "ndjson" | ndjson::ndjson_parse(.) | map(fn(x): x["id"];)' data.ndjson

API

ndjson_parse(input)

Parses an NDJSON string and returns an array of mq values. Empty and whitespace-only lines are ignored.

ndjson_parse_line(line)

Parses a single NDJSON line into a mq value. Leading and trailing whitespace is trimmed.

ndjson_stringify(data)

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.

Example

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.ndjson

Compatibility

Requires mq v0.5 or later.

License

MIT

About

An NDJSON (Newline Delimited JSON) / JSON Lines parser and serializer implemented as an mq module.

Topics

Resources

License

Stars

Watchers

Forks