I'm treading on unfamiliar ground here with my first REST driver
implementation. I've found that the driver policies mostly work on
standard XDS documents, as expected, up through the Output Transform.
Here, a policy (NETQRESTJSON-otp-XDStoJSON) calls Java (xdsToJSON) to
transform the current XDS to a JSON formatted object string.

This Java class seems somewhat inflexible in what it accepts (strings,
structures), undocumented, and very inflexible in what it puts out
(arrays of strings). Unfortunately for me, the REST service I need to
talk to has its own set of requirements for the JSON it will accept, and
arrays of strings aren't in its vocabulary.

An <add> document parsed by xdsToJSON produces a JSON like:

Code:
{
"lastName": "[Cleophus"],
"recordTypeId": ["2643230248141758"],
"externalId": ["rev1"],
"paths": [
{
"waitTime": ["0"],
"pathId": ["241901148045316"],
"value": ["seethelight@tripplerockchurch.org"]
},
{
"waitTime": ["0"],
"pathId": ["241901148045324"],
"countryCode": ["US"],
"value": ["8154441212"]
}
],
"firstName": ["Reverend"]
}
which is, I believe, *valid* JSON, but is not what this service expects.
The service insists that many of these things are single valued strings,
not arrays of strings, and that others are actually numerics, not strings
at all. So what I need is:

Code:
{
"lastName": "Cleophus",
"recordTypeId": 2643230248141758,
"externalId": "rev1",
"paths": [
{
"waitTime": 0,
"pathId": 241901148045316,
"value": "seethelight@tripplerockchurch.org"
},
{
"waitTime": 0,
"pathId": 241901148045324,
"countryCode": "US",
"value": "8154441212"
}
],
"firstName": "Reverend"
}
It appears that the easiest way to "fix" this is with ECMAScript. I've
added the JSON library to get the JSON.parse() and JSON.stringify()
functions. And I'm using the "reviver" feature of JSON.parse() to hack
the input in to the output I need. This:

Code:
/** Parse a JSON object and smash single valued arrays of strings down to
strings.
* @param {String} input JSON object string
* @type string
* @return a JSON object string
*/
function JSONArray2String(input)
{
// parse input string in to JSON
var arrayOfObjects = JSON.parse(input, function (key, value) {
if (key === '')
{
return value;
}
if (key === 'pathId' || key === 'recordTypeId')
{
return new Number(value);
}
if (typeof value[0] === 'string')
{
return value.toString();
}
return value;
});
return(JSON.stringify(arrayOfObjects,'','  '));
}
seems to handle the cases I need handled. I don't think it's perfect, but
it seems to work. I don't like the way I'm having to specify the keys
that need to be numeric, but I don't see any other way to handle the lack
of information here. Everything on the input is a string, there's nothing
else to indicate that some of these are numeric values, where others are
strings containing digits.

As I said. I'm in unfamiliar territory with this, so are there any
obvious things I'm missing here? Any better way to do this?


--
--------------------------------------------------------------------------
David Gersic dgersic_@_niu.edu
Knowledge Partner http://forums.microfocus.com

Please post questions in the forums. No support provided via email.
If you find this post helpful, please click on the star below.