Lexx Alolia
Lexx Alolia

Reputation: 33

fetching contents of a JSON Stream

Working on a bot in Node.js which expands on the data created by another bot. That Bot outputs all it's data to a JSON page https://mee6.xyz/levels/267482689529970698?json=1

But I can't see the console output of data produced by JSONStream. How can I get it so i can use it for my extension system?

var request = require('request')
  , JSONStream = require('JSONStream')
  , es = require('event-stream')

request({url: 'https://mee6.xyz/levels/267482689529970698?json=1'})
  .pipe(JSONStream.parse('rows.*'))
  .pipe(es.mapSync(function (data) {
    console.error(data)
var stream = JSONStream.parse(['rows', true, 'doc']) //rows, ANYTHING, doc

stream.on('data', function(data) {
  console.log('received:', data);
});
//emits anything from _before_ the first match
stream.on('header', function (data) {
  console.log('header:', data) // => {"total_rows":129,"offset":0}
})
  }))

Upvotes: 0

Views: 1912

Answers (1)

Antonio Narkevich
Antonio Narkevich

Reputation: 4326

There are a couple of problems. It seems that you have mixed 2 approaches described in JSONStream documentation.

First, the JSON you're requesting simply doesn't contain any fields with the name 'row' that is why this doesn't work: .pipe(JSONStream.parse('rows.*'))

To see the output you can do something like this:

request({url: 'https://mee6.xyz/levels/267482689529970698?json=1'})
	//So I'm getting all the players records
	.pipe(JSONStream.parse('players.*'))
	.pipe(es.mapSync(function (data) {
		console.log(data);
	}));

Checkout JSONStream and JSONPath docs.

The second is that this stream stream = JSONStream.parse(['rows', true, 'doc']) is created and then simply lost. You're note using it. So if you don't like the 1st way you can do:

var stream = JSONStream.parse(['players']);
stream.on('data', function(data) {
	console.log('received:', data);
});

stream.on('header', function (data) {
	console.log('header:', data);
});

//Pipe your request into created json stream
request({url: 'https://mee6.xyz/levels/267482689529970698?json=1'})
	.pipe(stream);

Hope this helps.

Upvotes: 2

Related Questions