Yes, I think in that case a KVM storing a set of paths to map would be good.
For example if the JSON payload is
{
"foo": "1",
"variables": {
"name": "xxxxxxxxxxxxxxxxxxxx",
"id": "xxxxxxxxxxxxxx"
}
}
Then you would want something like this in the KVM:
{"variables": { "id" : "sys" } }
And you’d need some JavaScript to walk the payload and modify it according to the map stored in the KVM.
This might sound exotic but it’s really just name replacement and it’s easy and fast with a recursive function in a regular JavaScript callout.
For the recursive walk, look here.
Here’s an example that runs in nodejs from the command line:
// ===================================================================
// mock context
var contextVars = { };
var context = {
getVariable : function(name) {
var value = contextVars[name];
console.log('GET %s = %s', name, value);
return value;
},
setVariable: function (name, value) {
console.log('SET %s := %s', name, value);
contextVars[name] = value;
}
};
// =================================================================
var source = {
"foo": "1",
"variables": {
"name": "xxxxxxxxxxxxxxxxxxxx",
"id": "xxxxxxxxxxxxxx"
}
};
var mapRules = {"variables": { "id" : "sys_id" } };
var walkObj = require('./walkObj.js');
console.log("RULES");
console.log(JSON.stringify(mapRules));
walkObj(mapRules,
'rule',
function(name, value) {
context.setVariable(name, value);
});
console.log("BEFORE");
console.log(JSON.stringify(source, null, 2) + '\n');
walkObj(source,
'',
function(fullpath, itemvalue, oldkey, obj) {
var ruleName = 'rule.' + fullpath;
var substitution = context.getVariable(ruleName);
if (substitution) {
obj[substitution] = itemvalue;
delete obj[oldkey];
}
});
console.log("AFTER");
console.log(JSON.stringify(source, null, 2) + '\n');
and here is the output showing the results:
$ ./mapProperties.js
RULES
{"variables":{"id":"sys_id"}}
SET rule.variables.id := sys_id
BEFORE
{
"foo": "1",
"variables": {
"name": "xxxxxxxxxxxxxxxxxxxx",
"id": "xxxxxxxxxxxxxx"
}
}
GET rule.foo = undefined
GET rule.variables.name = undefined
GET rule.variables.id = sys_id
AFTER
{
"foo": "1",
"variables": {
"name": "xxxxxxxxxxxxxxxxxxxx",
"sys_id": "xxxxxxxxxxxxxx"
}
}
That code won’t run “as is” within a JS callout. To get it to run within a JS callout, You would need to:
- remove the “require” and use an IncludeURL element in the JS policy for the walkObj.js function.
- remove the mock context
- get the “source” from JSON.parse(context.getVariable(‘message.content’))
- get the mapRules from a KVM read (and JSON.parse() THAT too)
But it shows the principle:
- codify the mapping rules in some way
- walk the object and apply the rules
- re-serialize the modified object back to… some other context variable