Hi Everyone,
I need to extract value of a field based on a condition from the below JSON object in the flow, Can you please help.
I need to value of field b based on the value of a, like in below, It should return 2 if I pass 1
[ { "a":"1", "b":"2" }, { "a":"x", "b":"y" } ]
thank you!!
Tried this code, but returns error org.mozilla.javascript.Undefined@25ef85d5.
var tempPayLoad = context.getVariable("Response"); var tempPayLoadObj = JSON.parse(tempPayLoad); var searchField = "EmailAddress"; var searchVal = "a@b.com"; var auditorId; for (var i in tempPayLoadObj ) { if (i.searchField == searchVal) { auditorId = i.AuditorID; //results.push(obj.list[i]); } } context.setVariable("auditorId",auditorId);
Hi Pranay
Your question is a pretty generic one, unrelated to Apigee Edge, really. This is just basic Javascript logic. Here's some code that does what you asked for:
var data = [ { "a":"1", "b":"2" }, { "a":"x", "b":"y" } ]; function findA(targetA) { var found = data.find(function(item){ return item.a == targetA; }); return (found) ? found.b : null; } var toFind = '1'; console.log('for %s, found %s', toFind, findA(toFind));
You can adapt the above to replace a and b with searchEmail and auditorId, or whatever you properties are. Also, you'll need to populate the data variable from ... wherever you get it.
User | Count |
---|---|
1 | |
1 | |
1 | |
1 | |
1 |