Parsing a JSON tree with JavaScript

475
January 25, 2017, at 3:17 PM

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 ?

Answer 1

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; }

READ ALSO
Display uploaded image

Display uploaded image

After some research What is the best place for storing uploaded images, SQL database or disk file system?, I decided to store the uploaded pictures on file system eg

402
How can i get specific value of a key to controller laravel through ajax

How can i get specific value of a key to controller laravel through ajax

Passing data to controller through ajax is like this data: {name:chris,age:20,_token:token}

370
jQuery .done() .fail()

jQuery .done() .fail()

What we want here is to manipulate the createSite function expected return which is {ac:failed} so when it failedfail(failOption) will be execute else sendMail /

374
Export checked item from DataBase to CSV

Export checked item from DataBase to CSV

I want to export checkbox:checked item from database to csv Using PHP & JqueryEverything is fine

453