Converting comma delimited JSON to a newline delimited node

kisonay picture kisonay · Sep 24, 2015 · Viewed 7.5k times · Source

I have a JSON file which I am reading with node, modifying and saving it as a json file.

I'm looking to save the new json as newline delimited vs being in an array.

I came across https://github.com/CrowdProcess/newline-json but don't fully understand streams. If i have the following stream setup, how can I pipe it though the parser and stringifier?

fileStream = fs.createReadStream('source.json')
writeStream = fs.createWriteStream('output.txt');

var Parser = require('newline-json').Parser;
var Stringifier = require('newline-json').Stringifier;

var parser = new Parser();
var stringifier = new Stringifier();

But running the following only outputs a blank file.

fileStream.pipe(parser).pipe(stringifier).pipe(writeStream)

what am I missing about streams?

Answer

peak picture peak · Sep 24, 2015

One way to convert a JSON array to a stream of newline-delimited JSON entities is to use jq with the -c option, e.g.

$ jq -c ".[]"

Input:

[[1,2], 3, {"4":5}]

Output:

[1,2]
3
{"4":5}

See https://stedolan.github.io/jq