GithubHelp home page GithubHelp logo

tilfin / jsonlines-processor Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 25 KB

JSON Lines streaming processor for CLI

License: MIT License

JavaScript 94.07% Shell 5.93%
jsonlines processor cli json-log

jsonlines-processor's Introduction

jsonlines-processor

NPM Version Build Status

JSON Lines streaming processor for CLI

A JSON Lines is called newline-delimited JSON and used for structured logs.

Install

$ npm install -g jsonlines-processor

How to Use

Create logic.js

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()
}

Run command

$ gunzip -c application-json.log.gz | jlp logic.js > output_json.log

Helper function

Following utility methods can be called in process, finalize, before or after functions

sort(items, [key], [direct])

Arguments

  • items:Array - The array to process
  • [key]:String - Target field name. item itself if not specified
  • [direct]:String - Ascending if not specified, else descending

Returns

  • Array - the new array of sorted items

keyBy(items, key)

Arguments

  • items:Array - The array to process
  • key:String - The iteratee to transform key

Returns

  • Object - the composed aggregate object.

sum(items, [key])

Arguments

  • items:Array - The array to process
  • [key]:String - Target field name. item itself if not specified

Returns

  • Number - the total value for each items

readJSONLinesFile(fileName)

Arguments

  • fileName:String - JSON Lines file path

Returns

  • Array - the new array of JSON object

readTSVFile(fileName)

Arguments

  • fileName:String - TSV file path

Returns

  • Array - Returns the new array of array item

Examples

example-json.log

{"name":"Hanako","age":16,"score":41}
{"name":"Taro","age":18,"score":81}
{"name":"Mike","age":15,"score":72}
{"name":"Ken","age":17,"score":90}

logic1.js

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')
}

Result

$ jlp logic1.js < example-json.log
{"name":"Ken",score:90}
{"name":"Taro",score:81}

License

MIT

jsonlines-processor's People

Contributors

tilfin avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.