Skip to content

ivangabriele/from-ndjson

Repository files navigation

from-ndjson

License CI Status Code Coverage NPM Version

Convert ndjson stringified data into JSON-object arrays.

Install

npm i -E from-ndjson

or:

yarn add -E from-ndjson

Usage

import fromNdjson from 'from-ndjson'

const ndjsonSample = `
  { "some": "thing" }
  { "foo": 17, "bar": false, "quux": true }
  { "may": { "include": "nested", "objects" : ["and", "arrays"] } }
`

const resultArray = fromNdjson(ndjsonSample)

console.log(resultArray[0])
// Output: { some: 'thing' }
console.log(resultArray[1])
// Output: { foo: 17, bar: false, quux: true }

API

fromNdjson(input, options?)

function fromNdjson(data: string, options: FromNdjson.Options)

Options

isStrict

Type: boolean
Default: 'false'

By default, fromNdjson() will just skip the rows that JSON.parse() can't parse. If you have an ndjson with 5 rows and 2 unparsable ones, the returned array will only contain 3 rows: the ones that could be parsed.

You can specify { isStrict: true } if you prefer an error to be thrown when a row is invalid.

Contribute

Please check the CONTRIBUTING document.