javascriptarraysnode.jsjsonjsonstream

JSON Stream Npm modules queries


I am working with a big JSON data and used Json stream module in my Nodejs to get the values.

This is my JSON Structure, I have to parse and collected 4 or 5 values from both metadata and status elements. I have 5 elements in the JSON array.

enter image description here

request({url:'ssss',verify:'False',headers:{'Authorization':'Bearer zzzz','Accept':'application/json','User-Agent':'zzz'}})
  .pipe(JSONStream.parse('items'))
  .pipe(es.mapSync(function (data) {
    console.log("Data "+data);
    console.log("Stringify "+ JSON.stringify(data));
   var specificValue = JSON.stringify(data);
    console.log("Specific Value"+ specificValue[0].metadata.labels.app);
    console.log("Parse and Stringify "+ JSON.parse(JSON.stringify(data)));
  })) ;

In console.log Data i can see 5 objects as [object Object],[object Object],[object Object],[object Object],[object Object]

In console.log Stringify I can only see a chunk (1 element) of JSON data, I do not see all 5 elements.

In console.log Specific Value I see TypeError: Cannot read property 'labels' of undefined. The label element is there displayed log and the query specificValue[0].metadata.labels.app is working other JSONPath testers.

How can I parse and get the specific value even after using JSON Stream module?

Ideally I would like to do a for loop and get all the values.

In console.log Parse and Stringify I get [object Object],[object Object],[object Object],[object Object],[object Object]


Solution

  • Comment out the line that raises the error and you will see that function(data) is actually called for each element in 'items' array. The error abruptly terminates your script.

    Each element matched by JSONStream.parse('items') will be sent through the stream. Thus, es.mapSync will be called multiple times. The "data" variable will actually be items[0] at first call, items[1] at second call etc.

    Try to log something from each item, for example use this:

    request({url:'ssss',verify:'False',headers:{'Authorization':'Bearer zzzz','Accept':'application/json','User-Agent':'zzz'}})
      .pipe(JSONStream.parse('items'))
      .pipe(es.mapSync(function (data) {
        console.log(data.metadata.labels.app);
    }));