dominictarr / JSONStream

rawStream.pipe(JSONStream.parse()).pipe(streamOfObjects)
Other
1.91k stars 165 forks source link

more examples? #115

Open brandonmp opened 7 years ago

brandonmp commented 7 years ago

Haven't worked too much with streams & having some trouble understanding this API--anyone have some good examples they can point to?

my use case is pretty simple. I have a giant single-object JSON, ie,

{
  users: {
       uid1: {
          // etc etc
        }, 
       uid2: {
         // etc etc
       }  
  }
}

So, there are no arrays, but just a massive nested object.

I'd like to stream it in, parse it, and write the results to a new JSON.

So far, this is what I've come up w/:

 // write all objects with a path that matches  '/*/images/'
var writeStream = fs.createWriteStream('home-images.json')
var readStream = fs.createReadStream('homes.json')
readStream
  .pipe(JSONStream.parse([true, 'images', {emitKey: true}]))
  .pipe(writeStream)

But as you may have guessed, this is just making it rain errors.

ypeError: Invalid non-string/buffer chunk
    at validChunk (_stream_writable.js:216:10)
    at WriteStream.Writable.write (_stream_writable.js:245:12)
    at Stream.ondata (stream.js:31:26)
    at emitOne (events.js:96:13)
    at Stream.emit (events.js:188:7)
    at drain (/home/bmp/code/wayhome/database-utils/node_modules/through/index.js:36:16)
    at Stream.stream.queue.stream.push (/home/bmp/code/wayhome/database-utils/node_modules/through/index.js:45:5)
    at Parser.parser.onValue (/home/bmp/code/wayhome/database-utils/node_modules/JSONStream/index.js:103:16)
    at Parser.proto.emit (/home/bmp/code/wayhome/database-utils/node_modules/jsonparse/jsonparse.js:265:8)
    at Parser.proto.pop (/home/bmp/code/wayhome/database-utils/node_modules/jsonparse/jsonparse.js:260:8)
dominictarr commented 7 years ago

you can't write javascript objects directly to a file - instead use JSONStream.stringify()

fpereira1 commented 7 years ago

I had to do something similar with this library and it worked well. The only thing is that JSONStram.stringify() writes an array of objects, which wasn't an issue for me.

Here's what I would try:

// write all objects with a path that matches  '/*/images/'
var writeStream = fs.createWriteStream('home-images.json')
var readStream = fs.createReadStream('homes.json')
readStream
  .pipe(JSONStream.parse([true, 'images', {emitKey: true}]))
  .pipe(JSONStream.stringify())
  .pipe(writeStream)