1

I want to search a JSON object to check if it contains string values stored in an array. and figure out the parent elements.

var searchVal = ['name_1a','name_2y','name_3x'];

var json = {
"location": {
        "title1x": {
            "1": "name_1x",
            "2": "name_2x",
            "3": "name_3x",
        },
       "title2y": {
            "1": "name_1y",
            "2": "name_2y",
            "3": "name_3y",
        },
    }

    "object": {
        "title1a": {
            "1": "name_1z",
            "2": "name_2z",
            "3": "name_3z",
        },
       "title2b": {
            "1": "name_1a",
            "2": "name_2a",
            "3": "name_3a",
        },
    }
};

I want to pass the results into a function. And deal with them separate.

name_1a -> function(title2b, object)
name_2y -> function(title2y, object)
name_3x -> function(title1x, location) etc.

. This is what I have tried so far. I can't seem to figure out how to gothrough the entire JSON object

var searchVal = ['name_1a','name_2y','name_3x'];
  for (var i=0 ; i < searchVal.length ; i++){
    for (var k=0 ; k < ????.length ; k++)
    {
      if (json.???????? == searchVal[i]) {
        results.push(???????);
        console.log(results);

      }
    }
  }
D3181
  • 2,037
  • 5
  • 19
  • 44
user2557850
  • 119
  • 1
  • 3
  • 14

3 Answers3

4

with the code below you can find what you are looking for recursively:

var json = {
    "location": {
        "title1x": {
            "1": "name_1x",
            "2": "name_2x",
            "3": "name_3x",
        },
       "title2y": {
            "1": "name_1y",
            "2": "name_2y",
            "3": "name_3y",
        },
    },
    "object": {
        "title1a": {
            "1": "name_1z",
            "2": "name_2z",
            "3": "name_3z",
        },
       "title2b": {
            "1": "name_1a",
            "2": "name_2a",
            "3": "name_3a",
        },
    }
};

var searchTest = function(varToSearch, jsonData) {

    for (var key in jsonData) {
        if(typeof(jsonData[key]) === 'object') {
            searchTest(varToSearch, jsonData[key]);
        } else {
            if(jsonData[key] == varToSearch) {
                console.log(jsonData[key]);
            }
        }
    }

}

searchTest('name_1a', json);

Reference:

get data from dynamic key value in json

get value from json with dynamic key

https://trinitytuts.com/tips/get-dynamic-keys-from-json-data/

How do I enumerate the properties of a JavaScript object?

Check if a value is an object in JavaScript

Community
  • 1
  • 1
Andre Rodrigues
  • 253
  • 3
  • 6
1

What about something like this:

var json = {
  "location": {
    "title1x": {
      "1": "name_1x",
      "2": "name_2x",
      "3": "name_3x",
    },
    "title2y": {
      "1": "name_1y",
      "2": "name_2y",
      "3": "name_3y",
    },
  },
  "object": {
    "title1a": {
      "1": "name_1z",
      "2": "name_2z",
      "3": "name_3z",
    },
    "title2b": {
      "1": "name_1a",
      "2": "name_2a",
      "3": "name_3a",
      "foo": [{
        "x": "aaa",
        "y": "bbb",
        "z": {
          "k": "name_3y"
        }
      }, {
        "x": "aaa",
        "y": "bbb",
        "z": {
          "k": "name_3y",
          "bar": [{
            "op": "test",
            "fooAgain": {
              "x": "name_3y"
            }
          }]
        }
      }]
    },
  }
};

function search(what, where) {

  var results = [];
  var parentStack = [];

  var searchR = function(what, where) {

    if (typeof where == "object") {
      parentStack.push(where);
      for (key in where) {
        searchR(what, where[key]);
      };
      parentStack.pop();
    } else {
      // here comes your search
      if (what === where) {
        results.push({
          parent: parentStack[parentStack.length - 1],
          value: where
        });
      }
    }

  }

  searchR(what, where);

  return results;

}

search("name_3y", json).forEach(function(value, key) {

  var out = "parent: \n";

  for (key in value.parent) {
    out += "    key: " + key + " - value: " + value.parent[key] + "\n";
  }

  out += "\nvalue: " + value.value;

  alert(out);

});

The search function will search for a value that is exactly equal inside a json object. You can use it to search for each element of an array for example, just adjust the code. A stack was necessary, since we need to keep track of the parents. I modified your json to insert more levels. The values of the results are objects with two attributes. I think that with this you can do what you need. You can, of course, modify my code to use regular expressions intead of strings in your search. It would be more powerfull.

davidbuzatto
  • 9,207
  • 1
  • 43
  • 50
0

var searchVal = ['name_1a','name_2y','name_3x'];

var json = {
"location": {
        "title1x": {
            "1": "name_1x",
            "2": "name_2x",
            "3": "name_3x",
        },
       "title2y": {
            "1": "name_1y",
            "2": "name_2y",
            "3": "name_3y",
        }
    },

    "object": {
        "title1a": {
            "1": "name_1z",
            "2": "name_2z",
            "3": "name_3z",
        },
       "title2b": {
            "1": "name_1a",
            "2": "name_2a",
            "3": "name_3a",
        }
    }
};
var getTitle=function(json,val){
  for (var key in json) {
    var titles= json[key];
    for (var tit in titles) {
      var names=titles[tit];
      for (var name in names) {
        var string=names[name];
        if(string===val)
          return tit;
      }
    }
}
}

searchVal.forEach(function(valToSearch){
   console.log(getTitle(json,valToSearch));
});
LellisMoon
  • 4,810
  • 2
  • 12
  • 24