parsed-stream2.js 648 B

123456789101112131415161718192021
  1. //////////////////////////////////////////
  2. // This example illustrates a more complex
  3. // example of parsing a JSON stream.
  4. //////////////////////////////////////////
  5. var needle = require('./../'),
  6. JSONStream = require('JSONStream');
  7. var url = 'http://jsonplaceholder.typicode.com/db';
  8. // Initialize our GET request with our default (JSON)
  9. // parsers disabled.
  10. var json = new needle.get(url, {parse: false})
  11. // And now interpret the stream as JSON, returning only the
  12. // title of all the posts.
  13. .pipe(new JSONStream.parse('posts.*.title'));
  14. json.on('data', function (obj) {
  15. console.log('got title: \'' + obj + '\'');
  16. })