0

I am trying to search the below Yodlee JSON datafeed for example if keyword is "furniture" then only display "plainTextDescription" which equals to "furniture transactions"

Not quite sure how to grep through nested JSON resultsets using JQuery...

///RAW YODLEE JSON RESULTSET

http://pastebin.com/6498mZJf

Thanks in advance!

Dango
  • 149
  • 2
  • 14

2 Answers2

1

Here's the Fiddle.

The short version is this:

var data = JSON.stringify({ "Body": [{...my obscenely long JSON}] });
var parsedData = JSON.parse(data);
var transactions = [];

// processes account objects
function processAccount(account) {
    if (account.cardTransactions) {
        for (var i = 0; i < account.cardTransactions.length; i++) {
            var transaction = account.cardTransactions[i];
            if (transaction) {
                if (transaction.categorizationKeyword.toLowerCase() === 'shell oil') {
                    transactions.push(transaction);
                }
            }
        }
    }
}

// processes the itemData objects
function processItemData(itemData) {
    for (var i = 0; i < itemData.accounts.length; i++) {
        processAccount(itemData.accounts[i]);
    }
}

(function() {
    // iterates through elements in the body
    for (var i = 0; i < parsedData.Body.length; i++) {
        processItemData(parsedData.Body[i].itemData);
    }

    for (var j = 0; j < transactions.length; j++) {
        $('#container').append('<p>' + transactions[j].plainTextDescription + '</p>');
    }
})();

I broke the bits into multiple functions for easier reading, but one could re-factor it to be less verbose.

m.casey
  • 2,579
  • 17
  • 10
0

You can accomplish this through JQuery very easily.

In the accepted answer to this question, a simple method has been written to do this for you. It does a deep search on the JSON object and then outputs all of the matching objects into an array. Getting the length of this array will give you the number of occurrences like you would like.

    function getObjects(obj, key, val) {
        var objects = [];
        for (var i in obj) {
            if (!obj.hasOwnProperty(i)) continue;
            if (typeof obj[i] == 'object') {
                objects = objects.concat(getObjects(obj[i], key, val));
            } else if (i == key && obj[key] == val) {
                objects.push(obj);
            }
        }
        return objects;
    }
    //put in the desired object name for ObjectName
    //if the object is a string just use JSON.parse(ObjectName) to convert it to a javascript object
    getObjects(ObjectName, "furnitire", "Furniture Transactions").length;
Community
  • 1
  • 1