Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I want to parse this JSON tree on the basis of the 'Name' attribute and return the matched node/nodes as an array of objects.

Kindly paste the JSON tree in here - https://jsonformatter.curiousconcept.com/ to get a better visualization of it.

The scenario would be such that, if the user enters 'Rule', all nodes that contain 'Rule*' corresponding to the 'Name' attribute would be returned.

To elaborate, the match would be such that if (object.Name.includes('Rule')) it would be valid match.

Since the JSON tree is huge and has children embedded within children, I was using Defiant.js and the function was built like this -

$scope.doASearch = function(elm) {
        var as = '//*[contains(Name, ' + '"' + elm + '"' + ')]';
        $scope.found = JSON.search($scope.treedata, as);
        $scope.treedata = _.uniq($scope.found, 'MetaCatID');
};

Since DefiantJS does not work on Microsoft's Edge Browser, switching to a compalitibility mode like IE-10 makes DefiantJS work but is breaking few other things. So, I had to rule out DefiantJS.

Is another JSON parsing library available to help me out or a JavaScript or jQuery solution which can do it me ?

share|improve this question
3  
how about you post a "minimal" version of the JSON tree in the question itself, rather than a link that will go stale once you get your answer, no doubt – Jaromanda X 31 mins ago
    
@JaromandaX - Kindly paste the JSON tree on jsonformatter.curiousconcept.com to get a better view of it. – CodeWalker 29 mins ago
    
what is the problem with JSON.parse()? – smnbbrv 28 mins ago
    
@smnbbrv - JSON.parse() works but I find it difficult to crawl across each node and child node of the tree. – CodeWalker 26 mins ago
1  
what is difficult in there? Just a recursive function with couple of if statements – smnbbrv 25 mins ago

You could use an iterative and recursive approach by checking the types of the items and iterate accordingly.

This proposal uses a callback for checking the object and return the actual object if condition is met.

function search(array, fn) {
    var result = [];
    array.forEach(function iter(o) {
        if (fn(o)) {
            result.push(o);
            return;
        }
        if (Array.isArray(o)) {
            o.forEach(iter);
            return;
        }
        if (o && typeof o === 'object') {
            Object.keys(o).forEach(function (k) {
                iter(o[k]);
            });
        }
    });
    return result;
}

var data = [{ tuple: { old: { MetaCategory: { MetaCatID: 517, ParentMetaCatRef: 0, Name: "D Application" } } }, MetaCatID: 517, ParentMetaCatRef: 0, Name: "D Application", subCategories: [{ tuple: { old: { MetaCategory: { MetaCatID: 518, ParentMetaCatRef: 517, Name: "Compass" } } }, MetaCatID: 518, ParentMetaCatRef: 517, Name: "Compass" }, { tuple: { old: { MetaCategory: { MetaCatID: 519, ParentMetaCatRef: 517, Name: "Orbe" } } }, MetaCatID: 519, ParentMetaCatRef: 517, Name: "Orbe" }, { tuple: { old: { MetaCategory: { MetaCatID: 520, ParentMetaCatRef: 517, Name: "PSI" } } }, MetaCatID: 520, ParentMetaCatRef: 517, Name: "PSI" }, { tuple: { old: { MetaCategory: { MetaCatID: 521, ParentMetaCatRef: 517, Name: "SAP" } } }, MetaCatID: 521, ParentMetaCatRef: 517, Name: "SAP" }] }, { tuple: { old: { MetaCategory: { MetaCatID: 541, ParentMetaCatRef: 0, Name: "D Versions" } } }, MetaCatID: 541, ParentMetaCatRef: 0, Name: "D Versions", subCategories: [{ tuple: { old: { MetaCategory: { MetaCatID: 542, ParentMetaCatRef: 541, Name: "Baseline 2016-12-31" } } }, MetaCatID: 542, ParentMetaCatRef: 541, Name: "Baseline 2016-12-31" }, { tuple: { old: { MetaCategory: { MetaCatID: 543, ParentMetaCatRef: 541, Name: "CLS step 3 2017-04-15" } } }, MetaCatID: 543, ParentMetaCatRef: 541, Name: "CLS step 3 2017-04-15" }] }, { tuple: { old: { MetaCategory: { MetaCatID: 365, ParentMetaCatRef: 0, Name: "Market" } } }, MetaCatID: 365, ParentMetaCatRef: 0, Name: "Market", subCategories: [{ tuple: { old: { MetaCategory: { MetaCatID: 366, ParentMetaCatRef: 365, Name: "Sector" } } }, MetaCatID: 366, ParentMetaCatRef: 365, Name: "Sector", subCategories: [{ tuple: { old: { MetaCategory: { MetaCatID: 463, ParentMetaCatRef: 366, Name: "term" } } }, MetaCatID: 463, ParentMetaCatRef: 366, Name: "term" }, { tuple: { old: { MetaCategory: { MetaCatID: 464, ParentMetaCatRef: 366, Name: "category" } } }, MetaCatID: 464, ParentMetaCatRef: 366, Name: "category" }, { tuple: { old: { MetaCategory: { MetaCatID: 367, ParentMetaCatRef: 366, Name: "Subsector" } } }, MetaCatID: 367, ParentMetaCatRef: 366, Name: "Subsector" }] }] }];

console.log(search(data, function (o) {
    return o.MetaCatID === 367;
}));
.as-console-wrapper { max-height: 100% !important; top: 0; }

share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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