@stdlib/fs-read-ndjson
v0.1.1
Published
Read a file as newline-delimited JSON.
Readme
readNDJSON
Read a file as newline-delimited JSON.
Installation
npm install @stdlib/fs-read-ndjsonUsage
var readNDJSON = require( '@stdlib/fs-read-ndjson' );readNDJSON( file[, options], clbk )
Asynchronously reads a file as newline-delimited JSON.
var join = require( 'path' ).join;
readNDJSON( join( __dirname, 'examples', 'fixtures', 'file.ndjson' ), clbk );
function clbk( error, data ) {
if ( error ) {
throw error;
}
console.log( data );
}The function accepts the following options:
- encoding: file encoding.
- flag: file status flag.
- reviver: JSON transformation function.
The options parameter may also be a string specifying the file encoding.
var join = require( 'path' ).join;
readNDJSON( join( __dirname, 'examples', 'fixtures', 'file.ndjson' ), 'utf8', clbk );
function clbk( error, data ) {
if ( error ) {
throw error;
}
console.log( data );
}readNDJSON.sync( file[, options] )
Synchronously reads a file as newline-delimited JSON.
var join = require( 'path' ).join;
var instanceOf = require( '@stdlib/assert-instance-of' );
var out = readNDJSON.sync( join( __dirname, 'examples', 'fixtures', 'file.ndjson' ) );
if ( instanceOf( out, Error ) ) {
throw out;
}
console.log( out );The function accepts the same options as readNDJSON() above.
Notes
- If the
encodingoption is set toutf8and the file has a UTF-8 byte order mark (BOM), the byte order mark is removed before attempting to parse as newline-delimited JSON.
Examples
var join = require( 'path' ).join;
var readNDJSON = require( '@stdlib/fs-read-ndjson' );
var file = join( __dirname, 'examples', 'fixtures', 'file.ndjson' );
// Synchronously read file contents...
var data = readNDJSON.sync( file, 'utf8' );
// returns [...]
data = readNDJSON.sync( 'beepboop', {
'encoding': 'utf8'
});
// returns <Error>
// Asynchronously read file contents...
readNDJSON( file, clbk );
readNDJSON( 'beepboop', clbk );
function clbk( error, data ) {
if ( error ) {
if ( error.code === 'ENOENT' ) {
console.error( 'JSON file does not exist.' );
} else {
throw error;
}
} else {
console.log( 'Package description: %s', data[2].description );
}
}Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2026. The Stdlib Authors.
