Import jsonl (JSON lines) files as JSON array
npm install rollup-plugin-jsonlinesš£ A Rollup plugin which imports .jsonl (JSON Lines) files as JSON arrays.
This plugin requires an LTS Node version (v14.0.0+) and Rollup v1.20.0+.
Using npm:
``console`
npm install rollup-plugin-jsonlines --save-dev
Create a rollup.config.js configuration file and import the plugin:
`js
import jsonl from 'rollup-plugin-jsonlines';
export default {
input: 'src/index.js',
output: {
dir: 'output',
format: 'cjs'
},
plugins: [jsonl()]
};
`
Then call rollup either via the CLI or the API.
_vite.config.js_
`js
import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import jsonl from 'rollup-plugin-jsonlines';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [svelte(), jsonl()]
});
`
_fruits.jsonl_
`json`
{ "type": "apples", "count": 7 }
{ "type": "pears", "count": 4 }
{ "type": "bananas", "count": 5 }
_index.js_
`js
import fruit from './fruits.jsonl';
console.log(fruit);
`
_log_
`json`
[
{ "type": "apples", "count": 7 },
{ "type": "pears", "count": 4 },
{ "type": "bananas", "count": 5 }
]
Type: Booleanfalse
Default:
If set to false (default), an exception will be thrown in case of invalid JSON on a line.
If set to true, invalid lines will be omitted.
Type: Functionnull
Default:
Specifies a function which processes each row in the parsed array. The function can either manipulate the passed row, or return an entirely new row object.
This option could be used for converting numeric string values into Number values. ā for example turning numeric values into numbers, e.g.
`js``
jsonl({
processRow: (row, id) => {
Object.keys(row).forEach((key) => {
var value = row[key];
row[key] = isNaN(+value) ? value : +value;
});
}
});