Tuesday, 4 April 2017

Parsing quickbooks webhook json

I'm attempting to set up a webhook with Quickbooks and for testing I'm using a sample of the JSON they will post to the webhook.

The format of the JSON is as follows:

{
    "eventNotifications":[
    {
        "realmId":"1185883450",
        "dataChangeEvent":
        {
            "entities":[
            {
                "name":"Customer",
                "id":"1",
                "operation":"Create",
                "lastUpdated":"2015-10-05T14:42:19-0700"
            },
            {
                "name":"Vendor",
                "id":"1",
                "operation":"Create",
                "lastUpdated":"2015-10-05T14:42:19-0700"
            }]
        }
    }]
}

Using Javascript, I attempted to parse it a couple ways but I keep getting stuck on the entities part.

Attempt 1: With this attempt, I get an error that length is undefined, which led me to try the next attempt.

for (var i = 0; i < req.body.eventNotifications.length; i++) {

    // Works fine
    realmId.push(req.body.eventNotifications[i].realmId);

    for (var j = 0; j < req.body.eventNotifications[i].dataChangeEvent.entities.length; j++) {
        // length is undefined
    }
}

Attempt 2: I don't get the error as above but anything I try to access underneath entities is undefined.

for (var i = 0; i < req.body.eventNotifications.length; i++) {

    // Works fine
    realmId.push(req.body.eventNotifications[i].realmId);

    for (var j = 0; j < req.body.eventNotifications[i].dataChangeEvent.length; j++) {
        // Anything I try to access is undefined
        var test = req.body.eventNotifications[i].dataChangeEvent[j].entities.name;
    }
}



via Mr.Smithyyy

No comments:

Post a Comment