Code Monkey home page Code Monkey logo

Comments (7)

juliangruber avatar juliangruber commented on August 15, 2024

you can only write strings or buffers to binary streams like process.stdout

from jsonstream.

q2dg avatar q2dg commented on August 15, 2024

Sorry, I don't understand you.
If a change this line:

var stream=rq({url: "http://isaacs.couchone.com/registry/_all_docs"})

by this line (namely, replacing a stream filled from network by a stream filled from local filesystem):

var stream = fs.createReadStream("datos.json", {encoding: "utf8"})

piping to process.stdout works.

from jsonstream.

juliangruber avatar juliangruber commented on August 15, 2024

for reference, here's the code with syntax highlighting:

var js = require("JSONStream")
var rq = require("request")
var stream=rq({url: "http://isaacs.couchone.com/registry/_all_docs"})
var parser=js.parse("rows.*")
parser.on("data", function (dato) {
  console.log("\n----------------")
  console.log(dato)
})
stream.pipe(parser)

This code doesn't work:

var js = require("JSONStream")
var rq = require("request")
var stream=rq({url: "http://isaacs.couchone.com/registry/_all_docs"})
var parser=js.parse("rows.*")
parser.on("data", function (dato) {
  console.log("\n----------------")
})
stream.pipe(parser).pipe(process.stdout)

from jsonstream.

juliangruber avatar juliangruber commented on August 15, 2024

In the first case, you're not piping anything to process.stdout. It's legal to do console.log({some:'object'}) but not process.stdout.write({some:'object'}), it has to be process.stdout.write("{some:'object'}").

from jsonstream.

q2dg avatar q2dg commented on August 15, 2024

Oooh, thanks!!! Now I understand!!

So I've include a JSON.stringify() before piping to process.stdout and now it works! Thanks!!

var js = require("JSONStream")
var rq = require("request")
var through = require("through")
var stream=rq({url: "http://isaacs.couchone.com/registry/_all_docs"})
var parser=js.parse("rows.*")
parser.on("data", function (dato) {
console.log("\n----------------")
})

var tr= through(function(dato) {
this.queue(JSON.stringify(dato))
})
stream.pipe(parser).pipe(tr).pipe(process.stdout)

from jsonstream.

juliangruber avatar juliangruber commented on August 15, 2024

exchange the through stream with js.stringify() and you have one dependency less :)

from jsonstream.

q2dg avatar q2dg commented on August 15, 2024

Aaah, ok.
I was proving js.stringify() previously but I didn't understand what kind of data it returns. Afortunately, I've seen that putting a false value as parameter I can get what I want, so changing (as you say) last line with

stream.pipe(parser).pipe(js.stringify(false)).pipe(process.stdout)

it works too. Yes!

Thanks a lot!!!!

from jsonstream.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.