JSON Lines streaming processor for CLI
A JSON Lines is called newline-delimited JSON and used for structured logs.
$ npm install -g jsonlines-processor
exports.process = async function(item) {
// filter
if (item.target === 'my_require') {
return item;
} else if (item instanceof Array) {
// Return multiple items
item.forEach(it => {
if (it.target === 'my_require') {
this.push(it);
}
});
}
}
// Optional
exports.finalize = async function(items) {
// sort
return this.sort(items, 'age')
}
exports.before = async function(cliArg1, cliArg2) {
// const anotherLogs = await this.readJSONLinesFile('./another.log')
// const userMap = this.keyBy(anotherLogs, 'user.name')
// await startServer()
}
exports.after = async function(items) {
// await shutdownServer()
}
$ gunzip -c application-json.log.gz | jlp logic.js > output_json.log
Following utility methods can be called in process
, finalize
, before
or after
functions
items:Array
- The array to process[key]:String
- Target field name. item itself if not specified[direct]:String
- Ascending if not specified, else descending
Array
- the new array of sorted items
items:Array
- The array to processkey:String
- The iteratee to transform key
Object
- the composed aggregate object.
items:Array
- The array to process[key]:String
- Target field name. item itself if not specified
Number
- the total value for each items
fileName:String
- JSON Lines file path
Array
- the new array of JSON object
fileName:String
- TSV file path
Array
- Returns the new array of array item
{"name":"Hanako","age":16,"score":41}
{"name":"Taro","age":18,"score":81}
{"name":"Mike","age":15,"score":72}
{"name":"Ken","age":17,"score":90}
Extracting the name and score of only item whose age is greater than 16, and sorting items by their score in descending order
exports.process = async ({ name, age, score }) => {
if (age > 16) {
return { name, score }
}
}
exports.finalize = async function(items) {
return this.sort(items, 'score', 'desc')
}
$ jlp logic1.js < example-json.log
{"name":"Ken",score:90}
{"name":"Taro",score:81}