I have a Json string in KVM and my objective is to compare a value in KVM JSON string with request Json payload parameter value.
I have tried multiple solutions provided in earlier posts in apigee community
https://community.apigee.com/articles/55885/how-can-you-extract-all-values-from-a-json-hash-in.html
But getting run time error : walkObj definition not found
var variableName ="message.content";var hash = JSON.parse(context.getVariable(variableName)); walkObj(hash,'json',function(name, value){ context.setVariable(name, value);});
https://community.apigee.com/questions/57407/how-to-iterate-through-a-json-object-to-findreplac.html
not getting all keys in output - Its not iterating complete object and picking some keys randomly from Json .
Sample Json string stored in KVM is
[{"AT":[{"CC":"MD","RegionCode":"IMUS","Monday":{"DownTime1":"00:00:00","UpTime1":"02:00:00","DownTime2":"00:00:00","UpTime2":"01:50:00"},"Tuesday":{"DownTime1":"00:00:00","UpTime1":"02:00:00","DownTime2":"00:10:00","UpTime2":"01:50:00"},"Wednesday":{"DownTime1":"00:00:00","UpTime1":"02:00:00","DownTime2":"00:10:00","UpTime2":"01:50:00"},"Thursday":{"DownTime1":"00:00:00","UpTime1":"02:00:00","DownTime2":"00:10:00","UpTime2":"01:50:00"},"Friday":{"DownTime1":"00:00:00","UpTime1":"02:00:00","DownTime2":"00:10:00","UpTime2":"01:50:00"},"Saturday":{"DownTime1":"00:00:00","UpTime1":"02:00:00","DownTime2":"20:00:00","UpTime2":"23:59:59"},"Sunday":{"DownTime1":"20:00:00","UpTime1":"23:59:59","DownTime2":"00:00:00","UpTime2":"00:35:00"}},{"CC":"FT","RegionCode":"IMCA","Monday":{"DownTime1":"23:00:00","UpTime1":"23:59:59","DownTime2":"23:10:00","UpTime2":"23:59:58"},"Tuesday":{"DownTime1":"23:00:00","UpTime1":"23:59:59","DownTime2":"00:00:00","UpTime2":"01:00:00"},"Wednesday":{"DownTime1":"23:00:00","UpTime1":"23:59:59","DownTime2":"00:00:00","UpTime2":"01:00:00"},"Thursday":{"DownTime1":"23:00:00","UpTime1":"23:59:59","DownTime2":"00:00:00","UpTime2":"01:00:00"},"Friday":{"DownTime1":"23:00:00","UpTime1":"23:59:59","DownTime2":"00:00:00","UpTime2":"01:00:00"},"Saturday":{"DownTime1":"18:00:00","UpTime1":"23:59:59","DownTime2":"00:00:00","UpTime2":"01:00:00"},"Sunday":{"DownTime1":"19:00:00","UpTime1":"23:25:00","DownTime2":"00:00:00","UpTime2":"00:23:00"}},{"CC":"UK","RegionCode":"IMUK","Monday":{"DownTime1":"18:00:00","UpTime1":"18:45:00","DownTime2":"18:10:00","UpTime2":"18:40:00"},"Tuesday":{"DownTime1":"18:00:00","UpTime1":"18:45:00","DownTime2":"18:10:00","UpTime2":"18:40:00"},"Wednesday":{"DownTime1":"18:00:00","UpTime1":"18:45:00","DownTime2":"18:10:00","UpTime2":"18:40:00"},"Thursday":{"DownTime1":"18:00:00","UpTime1":"18:45:00","DownTime2":"18:10:00","UpTime2":"18:40:00"},"Friday":{"DownTime1":"18:00:00","UpTime1":"18:45:00","DownTime2":"18:10:00","UpTime2":"18:40:00"},"Saturday":{"DownTime1":"13:00:00","UpTime1":"13:40:00","DownTime2":"19:50:00","UpTime2":"22:30:00"},"Sunday":{"DownTime1":"18:00:00","UpTime1":"18:45:00","DownTime2":"18:10:00","UpTime2":"18:40:00"}}]}]
********************************
I want to retrieve value of all the keys based on region code.
Thanks in advance 🙂
What I would do, if I were you, is build the logic outside the Apigee proxy and JS runtime. Build your JS logic in a nodejs script that you can iteratively test locally on your development workstation.
I want to retrieve value of all the keys based on region code.
This is a straightforward thing, in JavaScript. Totally independent of Apigee. You just have to navigate the data structure properly.
Your data looks like this:
var value1 = [ { "AT": [ { "CC": "MD", "RegionCode": "IMUS", "Monday": {...}, "Tuesday": {...}, "Wednesday": {...}, "Thursday": {...}, "Friday": {...}, "Saturday": {...}, "Sunday": {...} }, ...
In other words, it's an array, of length 1.
The first (only) item in the array is a hash, which has exactly one property, "AT".
The value of that property is itself an array.
Each item in the inner array corresponds to a particular region.
I think you want to select one of the items from the inner array based on the RegionCode.
This would do that:
function getRegionFinder(codeTioFind) { return function(item) { return (item.RegionCode == codeTioFind); }; } var imuk = value1[0].AT.find( getRegionFinder("IMUK") ); console.log(JSON.stringify(imuk, null, 2));
some notes:
That should get you going.
In your code you will want to do something like
var valueFromKvm = JSON.parse( context.getVariable( 'name_of_variable_holding_json_from_kvm');
and then use that value as
valueFromKvm[0].AT.find( ...)
And also you will want to check for null return from the find() function. If your RegionCode is not found, the result of find() will be null and your code should handle that.
Good luck.
ps: this is just a JavaScript question, very little here that is particular to Apigee.