Searching for text within JSON arrays

 
0 2 17K
2 REPLIES 2

What do you mean by "I just want all the arrays?"

I see only one array, the outer array.

I think maybe you mean "I want all the matching *elements* of the array." Can you confirm that?

And if so, also describe exactly how you would like to match? Only when the field "supported-environment" contains the word staging? Do you want to do a case insensitive match?

More detail and clarity, please!

I think you want something like this:

var a = [{
  "language-version": "",
  "languages-supported": "English",
  "owning-region": "Corporate",
  "peak-user-count": "",
  "product-department": "Risk and Compliance Management",
  "product-owner": "Johnson Paul",
  "product-owner-cc": "05444",
  "product-sponsor": "Dooley, Peter",
  "product-sponsor-cc": "05444",
  "product-sponsor-domain": " IT Security",
  "soa-classification": "service provider",
  "software-version": "6.2",
  "supported-environment": "Dev/Staging, Production",
  "supported-region": "Global",
  "unix-windows": "Windows"
}, {
  "language-version": "",
  "languages-supported": "English",
  "owning-region": "Corporate",
  "peak-user-count": "",
  "product-department": "Enterprise Architecture",
  "product-owner": "Brian D. Smith",
  "product-owner-cc": "05431",
  "product-sponsor": "Cynthia Czabala",
  "product-sponsor-cc": "",
  "product-sponsor-domain": "Enterprise Information Solutions and Customer Loyalty Marketing Technologies",
  "soa-classification": "service provider",
  "software-version": "2",
  "supported-environment": "Staging, Int, Production",
  "supported-region": "Corporate",
  "unix-windows": "Windows"
}];


var teststrings = [ 'stage', 'staging', 'smith', 'Corporate'];


function matcher(regexp) {
  return function (obj) {
  var found = false;
  Object.keys(obj).forEach(function(key){
    if ( ! found) {
      if ((typeof obj[key] == 'string') && regexp.exec(obj[key])) {
        found = true;
      }
    }
  });
  return found;
  };
}


teststrings.forEach(function(needle) {
  console.log('searching for: %s', needle);
  var re1 = new RegExp("\\b" + needle + "\\b", 'i');
  var matches = a.filter(matcher(re1));
  console.log('found count: %s', matches.length);
  console.log('found: %s', JSON.stringify(matches));
});

But Vidisha, this is just basic JavaScript.

This hasn't got anything to do with Apigee. I'm trying to help you but my main role is to help people with APIGEE, not general JavaScript questions. I encourage you to get more familiar with Java/Script Maybe take an online course where you can learn about array methods like .filter .map .forEach, and also learn about regular expression mapping. get familiar with what is an array, how to refer to the value of a property in a JS hash (it's not the second element in the array - it's the property value). And so on. Getting a basic level of competency will be very very helpful to you, in even framing the questions you want to ask. Also after going through a basic JS 101 course, you will be able to solve many of these issues yourself, without even asking for help.

I don't have any particular recommendations in that regard, but here is a search page: JS online course.