-1

I am receiving the JSON below from the following code in my Node js client:

res.on('data', function (chunk) {
        var parsedResponse = JSON.parse(JSON.stringify(chunk));
        console.log(parsedResponse);
    });

Console output:

{  "response" : {
"results" : [ {
  "events" : {
    "numAvlEvents" : 161,
    "numDtcEvents" : 0,
    "numJbusEvents" : 0,
    "numJbusDtcEvents" : 0,
    "numObdProvisionEvents" : 0,
    "numDeviceCommandResponseEvents" : 0,
    "avlEvents" : [ {
      "avlEvent" : {
        "deviceId" : 35,
        "eventTime" : "2015-02-13T08:05:40.000Z",
        "eventType" : "ALIVE",
        "gps" : {
          "gps" : {
            "carrier" : 410,
            "gpsValidity" : true,
            "hdop" : 4.0,
            "latitude" : 33.7974925,
            "longitude" : -85.16405,
            "odometer" : 0.0,
            "satellites" : 9
          }
        },
        "telemetry" : {
          "telemetry" : {
            "altitude" : 37327,
            "odometer" : 0.0,
            "speed" : 0
          }
        }
      }
    }, {
      "avlEvent" : {
        "deviceId" : 35,
        ...
      }
    } ]
   }
  } ]
 }
}

I cant seem to figure out how to access the the contents of the "avlEvents" array which seems to be nested inside the "events" object, which in turn is nested inside the "results" array, which in turn is nested inside the "response" object.

I've tried many iterations of parsedResponse.response.results['events'].avlEvents all yield some kind of error such as: Cannot read property 'results' of undefined. I'm a bit lost at this point, any help would be appreciatedd.

1 Answer 1

1

parsedResponse.response.results is a simple array and should be accessed with numerical indexes.

Thus, you want

parsedResponse.response.results[0].avlEvents

However, that's not really your problem.

You're reading from a stream. My guess is that you're trying to read a whole JSON string from that stream, but your data handler is dealing with chunks. Thus, there's a pretty good chance that you're not getting the complete JSON string in one chunk and so you're code isn't appropriate.

Also, I just realised that you're stringifying and then parsing your data which is crazy. Those methods are inverses of each other. Either you're getting a JSON string or you are not.

In this case, you're probably getting a JSON string so you just need to parse it, once you have it in its entirety.

3
  • Thanks for the feedback. I understand what you're saying about reading from the stream. But am unsure as to how exactly I retrieve the entire stream into one string before I parse it. Regarding the parse/stringinfy, if I use only JSON.parse I get an "Unexpected end of input" error (I'm now pretty sure is related to the stream issue), while if I use just JSON.stringify and get a string with all the new-line and escaped quotes inline (lots of \n \" in the output). Feb 13, 2015 at 16:57
  • The simplest approach is to concatenate the chunks together as they come in and delay your parsing until you get an 'end' event from your stream.
    – Dancrumb
    Feb 13, 2015 at 16:58
  • Perfect! @Dancrumb This worked perfectly: inline ` var eventsreq = https.request(options, function(res) { res.setEncoding('utf8'); var body = ''; stream.on('data', function (chunk) { body += chunk; }); stream.on('end', function () { var parsedResponse = JSON.parse(body); console.log(parsedResponse.response.results[0].events.avlEvents[0].avlEvent.gps.gps.latitude); }); });` Feb 13, 2015 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.