ndjson is useful for streaming values — the format
is essentially an array of objects, but (1) the outermost brackets [] are omitted so the array is implied, and (2) the separator between records is a newline instead of a comma. Basically a stream of lines where each line is a record in JSON format. The spec is not clear if a record/line can itself be an array, but objects can contain arrays.
Using the example presented in the spec, you must have received this stream of text in some way:
{"some":"thing"}
{"foo":17,"bar":false,"quux":true}
{"may":{"include":"nested","objects":["and","arrays"]}}
Let's say you've received this and stored it in a variable, which would be a string, input
. You could then break this string on newlines with input.split('\n')
parse each one via JSON.parse(…)
and save the result in an array.
let input = '{"some":"thing"}\n{"foo":17,"bar":false,"quux":true}\n{"may":{"include":"nested","objects":["and","arrays"]}}';
let result = input.split('\n').map(s => JSON.parse(s));
console.log('The resulting array of items:');
console.log(result);
console.log('Each item at a time:');
for (o of result) {
console.log("item:", o);
}