About stdlib...
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
Parse a string containing serialized newline-delimited JSON (NDJSON).
npm install @stdlib/utils-parse-ndjson
Alternatively,
- To load the package in a website via a
script
tag without installation and bundlers, use the ES Module available on theesm
branch (see README). - If you are using Deno, visit the
deno
branch (see README for usage intructions). - For use in Observable, or in browser/node environments, use the Universal Module Definition (UMD) build available on the
umd
branch (see README).
The branches.md file summarizes the available branches and displays a diagram illustrating their relationships.
To view installation and usage instructions specific to each branch build, be sure to explicitly navigate to the respective README files on each branch, as linked to above.
var parseNDJSON = require( '@stdlib/utils-parse-ndjson' );
Parses a string
as newline-delimited JSON
.
var out = parseNDJSON( '{"beep":"boop"}\n{"example":42}' );
// returns [ { 'beep': 'boop' }, { 'example': 42 } ]
To transform the string
being parsed, provide a reviver
.
function reviver( key, value ) {
if ( key === '' || key === 'beep' ) {
return ( typeof value === 'string' ) ? value.toUpperCase() : value;
}
return ( typeof value === 'number' ) ? value * 2 : value;
}
var str = '{"beep":"boop"}\n{"value": 20}\n{"numbers": [1,2,3]}';
var out = parseNDJSON( str, reviver );
// returns [ { 'beep' : 'BOOP' }, { 'value': 40 }, { 'numbers': [ 2, 4, 6 ] } ]
-
In contrast to the native
JSON.parse()
, this implementation parsesstring
asnewline-delimited JSON
and returns an array of parsed JSONs.var out = JSON.parse( '{"beep":"boop"}\n{"foo":"baz"}' ); // throws <SyntaxError> out = parseNDJSON( '{"beep":"boop"}\n{"foo":"baz"}' ); // returns [ { 'beep': 'boop' }, { 'foo': 'baz' } ]
-
In contrast to the native
JSON.parse()
, this implementation throws a TypeError if provided any value which is not astring
.var out = JSON.parse( null ); // returns null out = parseNDJSON( null ); // throws <TypeError>
-
In contrast to the native
JSON.parse()
, this implementation does not throw a SyntaxError if unable to parse a string as newline-delimited JSON.var out = parseNDJSON( '{"beep":boop}' ); // returns <SyntaxError> out = JSON.parse( '{"beep":boop}' ); // throws <SyntaxError>
-
In contrast to the native
JSON.parse()
, this implementation throws a TypeError if provided a reviver argument which is not a function.var out = JSON.parse( '{"a":"b"}', [] ); // returns { 'a': 'b' } out = parseNDJSON( '{"a":"b"}', [] ); // throws <TypeError>
var parseNDJSON = require( '@stdlib/utils-parse-ndjson' );
var out = parseNDJSON( '{"name":"John"}\n{"name":"Doe"}' );
// returns [ { 'name': 'John' }, { 'name': 'Doe' } ]
function reviver( key, value ) {
if ( key === 'name' ) {
return value.toUpperCase();
}
return value;
}
out = parseNDJSON( '{"name":"John"}\n{"name":"Doe"}', reviver );
// returns [ { 'name': 'JOHN' }, { 'name': 'DOE' } ]
out = parseNDJSON( '{"name":John}\n{"name":Doe}' );
// returns <SyntaxError>
out = parseNDJSON( ' ' );
// returns []
out = parseNDJSON( '{}' );
// returns [ {} ]
out = parseNDJSON( '{"name":"Eve"}\n42\ntrue\n[1,2,3]' );
// returns [ { 'name': 'Eve' }, 42, true, [ 1, 2, 3 ] ]
out = parseNDJSON( '{"name":"John"}\r\n{"name":"Doe"}' );
// returns [ { 'name': 'John' }, { 'name': 'Doe' } ]
out = parseNDJSON( '{"name":"John"}\n{"name":"Doe"}\n' );
// returns [ { 'name': 'John' }, { 'name': 'Doe' } ]
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.
See LICENSE.
Copyright © 2016-2024. The Stdlib Authors.