Search code examples
jsonnode.jsjqfsjsonlines

Converting comma delimited JSON to a newline delimited node


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?


Solution

  • 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