0

Is it possible to traverse through all the nodes and find the wanted field from a complex json whose level is not predetermined

{
    "_index": "test",
    "_type": "news",
    "_source": {
        "partnerName": "propertyFile 9",
        "relatedSources": "null",
        "entityCount": "50",
        "Categories": {
            "Types": {
                "Events": [{
                    "count": 1,
                    "term": "Time",
                    "Time": [{
                        "term": "Dec 9",
                        "Dec_9": [{
                            "count": 1,
                            "term": "2012"
                        }]
                        }]
                    }, {
                    "count": 4,
                    "term": "News",
                    "News": [{
                        "term": "Germany",
                        "Germany": [{
                            "count": 1,
                            "term": "Election"
                        }],
                        "currency": "Euro (EUR)"
                    }, {
                        "term": "Egypt",
                        "Egypt": [{
                            "count": 1,
                            "term": "Revolution"
                        }]
                        }]
                    }]
                }
        }
    }
}

I could get in to the first level , bt how do I go to second level if the field name is not present in the first level. This is how I started it. http://jsfiddle.net/W8qhA/1/

mVChr
  • 49,587
  • 11
  • 107
  • 104
user1371896
  • 2,192
  • 7
  • 23
  • 32
  • 1
    [Recursion](http://www.google.co.nz/search?sugexp=chrome,mod=14&sourceid=chrome&ie=UTF-8&q=recursion) – zerkms Jul 02 '12 at 05:40
  • @penartur I want to print the output of a given field name from text box..Ivnt added the textbox in fiddle..im jst giving values manually.. – user1371896 Jul 02 '12 at 05:57

3 Answers3

1

I hope this will solve your problem. There is some function and also a little explanation. Hope that helps you out. Traverse all the Nodes of a JSON Object Tree with JavaScript

Community
  • 1
  • 1
scx
  • 2,749
  • 2
  • 25
  • 39
1

I whipped up a function that will work on an object of the structure you presented. If there are multiple keys of the same name it will simply return the first one it encounters.

var findDeepKey = function(obj, key){
    var results = [];

    if (typeof obj !== 'object') return null;

    for (var k in obj) {
        if (k === key) return obj[k];
    }

    for (var k in obj) {
        if (typeof obj[k] === 'object') {
            if (obj[k].length) {
                for (var i = 0, il = obj[k].length; i < il; i++) {
                    results.push(findDeepKey(obj[k][i], key));
                }
            } else {
                for (var kk in obj[k]) {
                    if (kk === key) return obj[k][kk];
                    results.push(findDeepKey(obj[k][kk], key));
                }
            }
        }
    }

    for (var i = 0, il = results.length; i < il; i++) {
        if (results[i] !== null) return results[i];
    }

    return null;
};

See demo

mVChr
  • 49,587
  • 11
  • 107
  • 104
0

You can use http://james.newtonking.com/projects/json-net.aspx

Chandru velan
  • 136
  • 1
  • 3
  • 21