Skip to content
This repository has been archived by the owner on Dec 4, 2018. It is now read-only.

more examples? #115

Open
brandonmp opened this issue Dec 8, 2016 · 2 comments
Open

more examples? #115

brandonmp opened this issue Dec 8, 2016 · 2 comments

Comments

@brandonmp
Copy link

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
Copy link
Owner

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

@fpereira1
Copy link

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)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants