{"id":17664430,"url":"https://github.com/tilfin/jsonlines-processor","last_synced_at":"2025-03-30T12:16:51.566Z","repository":{"id":57285778,"uuid":"136207703","full_name":"tilfin/jsonlines-processor","owner":"tilfin","description":"JSON Lines streaming processor for CLI","archived":false,"fork":false,"pushed_at":"2019-10-19T15:28:05.000Z","size":25,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-08T02:48:27.133Z","etag":null,"topics":["cli","json-log","jsonlines","processor"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tilfin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-05T16:49:42.000Z","updated_at":"2021-09-25T02:00:54.000Z","dependencies_parsed_at":"2022-09-19T23:52:21.915Z","dependency_job_id":null,"html_url":"https://github.com/tilfin/jsonlines-processor","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tilfin%2Fjsonlines-processor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tilfin%2Fjsonlines-processor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tilfin%2Fjsonlines-processor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tilfin%2Fjsonlines-processor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tilfin","download_url":"https://codeload.github.com/tilfin/jsonlines-processor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246314151,"owners_count":20757463,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","json-log","jsonlines","processor"],"created_at":"2024-10-23T20:05:24.752Z","updated_at":"2025-03-30T12:16:51.547Z","avatar_url":"https://github.com/tilfin.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jsonlines-processor\n\n[![NPM Version][npm-image]][npm-url]\n[![Build Status](https://travis-ci.org/tilfin/jsonlines-processor.svg?branch=master)](https://travis-ci.org/tilfin/jsonlines-processor)\n\nJSON Lines streaming processor for CLI\n\nA JSON Lines is called newline-delimited JSON and used for structured logs.\n\n## Install\n\n```\n$ npm install -g jsonlines-processor\n```\n\n## How to Use\n\n### Create logic.js\n\n```js\nexports.process = async function(item) {\n  // filter\n  if (item.target === 'my_require') {\n    return item;\n  } else if (item instanceof Array) {\n    // Return multiple items\n    item.forEach(it =\u003e {\n      if (it.target === 'my_require') {\n        this.push(it);\n      }\n    });\n  }\n}\n\n// Optional\nexports.finalize = async function(items) {\n  // sort\n  return this.sort(items, 'age')\n}\n\nexports.before = async function(cliArg1, cliArg2) {\n  // const anotherLogs = await this.readJSONLinesFile('./another.log')\n  // const userMap = this.keyBy(anotherLogs, 'user.name')\n  // await startServer()\n}\n\nexports.after = async function(items) {\n  // await shutdownServer()\n}\n```\n\n### Run command\n\n```\n$ gunzip -c application-json.log.gz | jlp logic.js \u003e output_json.log\n```\n\n## Helper function\n\nFollowing utility methods can be called in `process`, `finalize`, `before` or `after` functions\n\n### sort(items, [key], [direct])\n\n#### Arguments\n\n* `items:Array` - The array to process\n* `[key]:String` - Target field name. item itself if not specified\n* `[direct]:String` - Ascending if not specified, else descending\n\n#### Returns\n\n* `Array` - the new array of sorted items\n\n### keyBy(items, key)\n\n#### Arguments\n\n* `items:Array` - The array to process\n* `key:String` - The iteratee to transform key\n\n#### Returns\n\n* `Object` - the composed aggregate object.\n\n### sum(items, [key])\n\n#### Arguments\n\n* `items:Array` - The array to process\n* `[key]:String` - Target field name. item itself if not specified\n\n#### Returns\n\n* `Number` - the total value for each items\n\n### readJSONLinesFile(fileName)\n\n#### Arguments\n\n* `fileName:String` - JSON Lines file path\n\n#### Returns\n\n* `Array` - the new array of JSON object\n\n### readTSVFile(fileName)\n\n#### Arguments\n\n* `fileName:String` - TSV file path\n\n#### Returns\n\n* `Array` - Returns the new array of array item\n\n## Examples\n\n#### example-json.log\n\n```json\n{\"name\":\"Hanako\",\"age\":16,\"score\":41}\n{\"name\":\"Taro\",\"age\":18,\"score\":81}\n{\"name\":\"Mike\",\"age\":15,\"score\":72}\n{\"name\":\"Ken\",\"age\":17,\"score\":90}\n```\n\n#### logic1.js\n\nExtracting the name and score of only item whose age is greater than 16, and sorting items by their score in descending order\n\n```js\nexports.process = async ({ name, age, score }) =\u003e {\n  if (age \u003e 16) {\n    return { name, score }\n  }\n}\n\nexports.finalize = async function(items) {\n  return this.sort(items, 'score', 'desc')\n}\n```\n\n#### Result\n\n```\n$ jlp logic1.js \u003c example-json.log\n{\"name\":\"Ken\",score:90}\n{\"name\":\"Taro\",score:81}\n```\n\n## License\n\n  [MIT](LICENSE)\n\n[npm-image]: https://img.shields.io/npm/v/jsonlines-processor.svg\n[npm-url]: https://npmjs.org/package/jsonlines-processor\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftilfin%2Fjsonlines-processor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftilfin%2Fjsonlines-processor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftilfin%2Fjsonlines-processor/lists"}