Skip to content Skip to sidebar Skip to footer

Iterate Through Nested Json Tree And Change Values

What I have is a JSON tree of the following structure: { 'projects': { 'Proj1': { 'milestones': { 'default': '20150101', 'de

Solution 1:

I ran into the same problem and this is my solution to iterate over a JSON object (MongoDB) and modify certain elements regardless of whether these are properties of the object or property is an array with more objects. I know the question is old, but it can be useful to someone. Assuming we have an object like this or even more complex.

var mixData = {
  "Hello": "World",
  "Foo" : "Bar",
  "sudo": [
    { "apt-get": "upgrade" , "force": false },
    { "apt-get": "update" , "force": true}
  ],
  "colors": ["blue","green"],
  "numbers":{
    "integer": [
      {"num": 1},
      {"num": 2},
      {"num": 3}
    ],
    "operator": "addition"
  } 
};

And we want to replace some string (blue in this case), but we don't know where is or what kind of constructor has our data.

// Detect if the element is an ArrayfunctionisElementArray(element){
  return (element.constructor === Array ? true : false);
}
//Detect if the element is an ObjectfunctionisElementObject(element){
  return (element.constructor === Object ? true : false);
}

Now that we have these two functions, we use a third to iterate over the item, regardless of whether an object or array.

functioniterate(element){
  //Check if the element is an Objectif(isElementObject(element)){
    for (var property in element){
      if(isElementObject(element[property])){
        element[property] = iterate(element[property]);
      }elseif(isElementArray(element[property])){
        //An arrayfor(var x = 0; x < element[property].length; x++){
          element[property][x] = iterate(element[property][x]);
        }
      }else{
        if(element.hasOwnProperty(property)){
          console.log("New object inside object property");
          element[property] = iterate(element[property]);
        }else{
          element[property] = replaceElement(element[property].toString());
          console.log("Single Element: " + element[property] )
          console.log(element + " " + element[property]);
        }
      }
    }
  }elseif(isElementArray(element)){
    //An Arrayfor (var x = 0; x < element.length; x++){
      element[x] = iterate(element[x]);
    }
  }else{
    //Single element in array or property
    element = replaceElement(element.toString());
    console.log("Single Element : " + element);
  }
  return element;
}

And the function we need to use to replace our string.

functionreplaceElement(element){
  if(element === "blue"){
    return"Blue is the warmest color"
  }else{
    return element;
  }
}

And finally, we can get the result:

console.log(iterate(mixData));

The result is:

{
    "Hello": "World",
    "Foo" : "Bar",
    "sudo": [
      { "apt-get": "upgrade" , "force": false },
      { "apt-get": "update" , "force": true}
    ],
    "colors": ["Blue is the warmest color","green"],
    "numbers":{
      "integer": [
        {"num": 1},
        {"num": 2},
        {"num": 3}
      ],
      "operator": "addition"
    } 
};

You can change the replace function to suit your needs. And of course, remove al the console logs.

Solution 2:

Your editing the passed value and not the original JSON object.

One way to fix this is to create a new JSON object, build as you iterate through the existing one, and then overwrite the original or use the new JSON object.

Another is to create a var holding the original JSON object and either pass it through your functions or access it directly inside the functions.

Post a Comment for "Iterate Through Nested Json Tree And Change Values"