How to use the parse function from JSONStream

Find comprehensive JavaScript JSONStream.parse code examples handpicked from public code repositorys.

JSONStream.parse is a Node.js module that provides a stream-based approach for parsing JSON data.

114
115
116
117
118
119
120
121
122
123
124
125
126
127
Utils.parseStream = function(stream, options, onRequest) {


  var onError = _.once(onRequest);
  var onSuccess = _.partial(onRequest, null);


  var result = JSONStream.parse();


  result.on('data', function(data) {


    // apply reviver walk function to prevent stringify/parse again
fork icon0
star icon0
watch icon2

+ 8 other calls in file

122
123
124
125
126
127
128
129
130
131
132
}


module.exports.json.stream = fetchJSONStream
function fetchJSONStream (uri, jsonPath, opts) {
  opts = config(opts)
  const parser = JSONStream.parse(jsonPath, opts.mapJson)
  const pt = parser.pipe(new PassThrough({objectMode: true}))
  parser.on('error', err => pt.emit('error', err))
  regFetch(uri, opts).then(res => {
    res.body.on('error', err => parser.emit('error', err))
fork icon0
star icon0
watch icon1

+ 4 other calls in file

How does JSONStream.parse work?

JSONStream.parse is a streaming JSON parser for Node.js that reads in a JSON stream of data in chunks and emits events for each JSON object encountered in the stream. It allows you to process large JSON data sets without having to load the entire dataset into memory at once.

141
142
143
144
145
146
147
148
149

/**
 * Output stream
 */

var stream = JSONStream.parse("*");
stream.on("data", function (data) {
  // create code snippet
  var snippet = createSnippet(data);
fork icon0
star icon0
watch icon1

+ 7 other calls in file

17
18
19
20
21
22
23
24
25
26
27
  return newlines ? newlines.length : 0;
}


module.exports = function (opts) {
    if (!opts) opts = {};
    var parser = opts.raw ? through.obj() : JSONStream.parse([ true ]);
    var stream = through.obj(
        function (buf, enc, next) { parser.write(buf); next() },
        function () { parser.end() }
    );
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const fs = require("fs");
const JSONStream = require("JSONStream");
const es = require("event-stream");

const inputStream = fs.createReadStream("large-data.json");
const parser = JSONStream.parse("*");
const filter = es.map((data, callback) => {
  // Filter the data here
  if (data.someProperty === "someValue") {
    callback(null, data);
  } else {
    callback();
  }
});
inputStream.pipe(parser).pipe(filter).pipe(process.stdout);

In this example, JSONStream.parse('*') creates a parser that can read any JSON object at the top-level. The parsed JSON is then piped through a filter stream which can filter the JSON objects based on some condition before outputting the result. Finally, the output is piped to process.stdout.