Skip to content

Latest commit

 

History

History
132 lines (104 loc) · 3.46 KB

README.md

File metadata and controls

132 lines (104 loc) · 3.46 KB

npm node deps cover chat size

jsonlines-loader

A webpack loader for parsing JSON Lines files into JavaScript objects.

JSON Lines files consist of zero to many 'lines' separated by linebreak characters '\n', with each line containing a valid JSON value. They are often used for log files or data output.

jsonlines-loader enables JSON Lines files, which usually have a ".jsonl" extension, to be imported directly into JavaScript. The contents of the file is delivered as an array of JavaScript objects, each corresponding to one line of the JSON Lines file. Note that the array starts at index 1, which is line 1 of the file; index 0 in the array is empty.

Getting Started

To begin, you'll need to install jsonlines-loader:

$ npm install jsonlines-loader --save-dev

You can use the loader either:

  • by configuring the jsonlines-loader in the module.rules object of the webpack configuration, or
  • by directly using the jsonlines-loader! prefix to the require statement.

Suppose we have the following jsonl file:

file.jsonl

// file.jsonl
{"source":"A","errors":13}
{"source":"B","errors":4}
{"source":"C","errors":984,"status":"critical"}

Usage with preconfigured rule

webpack.config.js

// webpack.config.js
module.exports = {
  entry: "./index.js",
  output: {
    /* ... */
  },
  module: {
    rules: [
      {
        // make all files ending in .jsonl use the `jsonlines-loader`
        test: /\.jsonl$/,
        use: "jsonlines-loader",
        type: "javascript/auto"
      }
    ]
  }
};
// index.js
var data = require("./file.jsonl");
// or, in ES6
// import data from './file.jsonl'

console.log(data[1].errors); // 13

Usage with require statement loader prefix

var data = require("jsonlines-loader!./file.jsonl");

console.log(data[2].errors); // 4

Options

ignoreParseErrors

Ordinarily the Webpack build will fail if any of the lines of the JSON Lines file does not contain a valid JSON value. If you set ignoreParseErrors: true then any line that does not contain a valid JSON value will simply be copied into the delivered array as a string.

// webpack.config.js
module: {
  rules: [
    {
      // make all files ending in .jsonl use the `jsonlines-loader`
      test: /\.jsonl$/,
      use: [
        (loader: "jsonlines-loader"),
        (options: { ignoreParseErrors: true })
      ],
      type: "javascript/auto"
    }
  ];
}

License

MIT