tirthankar1984 Trusted Contributor.
Trusted Contributor.
742 views

REST JSON format mismatch

Hi,
Customer have a REST full application, and the application accepts the below form-data JSON format,
{"username": "demouser5",
"password": "Password@123",
"confirmPassword": "Password@123",
"firstName": "testqw",
"lastName": "testps",
"phoneNumber": "9999119999",
"email": "dtestuserttqse@example.com",
"createdBy": 116,
"userStatus": "ENABLED",
"groupId": 1001,
"roleId": 101,
"customerId":1000,
"timezoneDescription":"(UTC-10:00) Hawaii"
}

I have configured a REST driver, and when an add event is generated, it sends the below raw-data
{"association":"MG00147852","city":["Bhopal"],"username":["MG00147852"],"customerId":["1000"],"firstName":["Mainak"],"Group Membership":[{"dn":"\\BSC_IDV_DEV\\bsc\\groups\\Universal_IoT","association":""}],"createdBy":["116"],"groupId":["1001"],"phoneNumber":["9999999999"],"roleId":["101"],"lastName":["Ghose"],"Title":["Universal_IoT"],"userStatus":["ENABLED"],"workforceID":["00147852"]}

For the above mismatch of JSON format user is not getting created in the application. So, can any please suggest us how to resolve the issue.
Labels (1)
0 Likes
6 Replies
Micro Focus Expert
Micro Focus Expert

Re: REST JSON format mismatch

On 2018-10-12 11:04, tirthankar1984 wrote:
>
> Hi,
> Customer have a REST full application, and the application accepts the
> below form-data JSON format,
> {"username": "demouser5",
> "password": "Password@123",
> "confirmPassword": "Password@123",
> "firstName": "testqw",
> "lastName": "testps",
> "phoneNumber": "9999119999",
> "email": "dtestuserttqse@example.com",
> "createdBy": 116,
> "userStatus": "ENABLED",
> "groupId": 1001,
> "roleId": 101,
> "customerId":1000,
> "timezoneDescription":"(UTC-10:00) Hawaii"
> }
>
> I have configured a REST driver, and when an add event is generated, it
> sends the below raw-data
> {"association":"MG00147852","city":["Bhopal"],"username":["MG00147852"],"customerId":["1000"],"firstName":["Mainak"],"Group
> Membership":[{"dn":"\\BSC_IDV_DEV\\bsc\\groups\\Universal_IoT","association":""}],"createdBy":["116"],"groupId":["1001"],"phoneNumber":["9999999999"],"roleId":["101"],"lastName":["Ghose"],"Title":["Universal_IoT"],"userStatus":["ENABLED"],"workforceID":["00147852"]}
>
> For the above mismatch of JSON format user is not getting created in the
> application. So, can any please suggest us how to resolve the issue.


You'll need to replace the out-of-the-box XML-to-JSON transformation
with a custom one.
I prefer doing it as ECMAScript function that is called with the
current-node as argument. This function uses DOM methods to parse the
input and build a JavaScript object. On return, the object is then
serialized into JSON.


--
Norbert
--
Norbert
0 Likes
jrmhscht Super Contributor.
Super Contributor.

Re: REST JSON format mismatch

I usually use ecmascript to convert the netiq generated json to what I need. Here is an example I am using which is similar to what you are asking for:
// Flatten functions take Netiq rest driver JSON and output simple "attr":"value" format

function FlattenModify(input) {
var inputParsed = JSON.parse(input);
var output = new Object();
var attr;
var n;
for (attr in inputParsed) {
if (Array.isArray(inputParsed[attr])) {
output[attr] = '';
for (n in inputParsed[attr]) {
if (!!inputParsed[attr] && inputParsed[attr].hasOwnProperty('add')) {
output[attr] += inputParsed[attr].add.toString();
output[attr] += ',';
}
}
output[attr] = output[attr].substr(0, output[attr].length-1);
}
else
{
if (!!inputParsed[attr] && inputParsed[attr].hasOwnProperty('add')) {
output[attr] = inputParsed[attr].add.toString();
}
}
}
return JSON.stringify(output);
}

function FlattenAdd(input) {
var inputParsed = JSON.parse(input);
var output = new Object();
var attr;
for (attr in inputParsed) {
output[attr] = inputParsed[attr].toString();
}
return JSON.stringify(output);
}


I call the functions with this rule at the end of the output transform:

<rule>
<description>Convert JSON to App format</description>
<conditions>
<and>
<if-class-name mode="regex" op="equal">user|org</if-class-name>
<if-xpath op="true">boolean(./request/value)</if-xpath>
</and>
</conditions>
<actions>
<do-set-local-variable name="netiqJSON" scope="policy">
<arg-string>
<token-xpath expression="../driver-operation-data/request/value/text()"/>
</arg-string>
</do-set-local-variable>
<do-if>
<arg-conditions>
<and>
<if-xpath op="true">@command="modify"</if-xpath>
</and>
</arg-conditions>
<arg-actions>
<do-set-local-variable name="appJSON" scope="policy">
<arg-string>
<token-xpath expression="es:FlattenModify($netiqJSON)"/>
</arg-string>
</do-set-local-variable>
</arg-actions>
<arg-actions>
<do-set-local-variable name="appJSON" scope="policy">
<arg-string>
<token-xpath expression="es:FlattenAdd($netiqJSON)"/>
</arg-string>
</do-set-local-variable>
</arg-actions>
</do-if>
<do-strip-xpath expression="../driver-operation-data/request/value"/>
<do-append-xml-element expression="../driver-operation-data/request[last()]" name="value"/>
<do-append-xml-text expression="../driver-operation-data/request/value[last()]">
<arg-string>
<token-local-variable name="appJSON"/>
</arg-string>
</do-append-xml-text>
</actions>
</rule>


I do something similar with query responses:

Ecmascript:
// input an application search response and build a netiq query result
// readAttr must be a string
function BuildQueryResults(input, readAttr) {
var inputParsed = JSON.parse(input);
if (inputParsed instanceof Array) {
inputParsed = inputParsed[0];
}
var results = new Object();
var attr;
if (typeof inputParsed !== "undefined") {
if (readAttr && (typeof readAttr !== "undefined")) {
if (inputParsed[readAttr] != null) {
results[readAttr] = inputParsed[readAttr].toString().split(',');
}
}
else {
for (attr in inputParsed) {
if (inputParsed[attr] != null) {
results[attr] = inputParsed[attr].toString().split(',');
}
}
}
}
if (results.id || results[readAttr]) {
var output = { "totalResults":1, "results":[results] }
}
else {
var output = { "totalResults":0, "results":[] }
}
return JSON.stringify(output);
}


Called from the input transform, before the JSON to XDS rule with this:

<rule>
<description>Convert query JSON format</description>
<comment xml:space="preserve">This converts the Application JSON to Netiq format. If there is a @read-attr present in the url-token it will only return that attribute.</comment>
<conditions>
<and>
<if-xpath op="true">driver-operation-data/@command="query"</if-xpath>
</and>
</conditions>
<actions>
<do-set-local-variable name="appJSON" scope="policy">
<arg-string>
<token-xpath expression="driver-operation-data/response/value/text()"/>
</arg-string>
</do-set-local-variable>
<do-set-local-variable name="readAttr" scope="policy">
<arg-string>
<token-xpath expression="driver-operation-data/response/url-token/@read-attr"/>
</arg-string>
</do-set-local-variable>
<do-set-local-variable name="queryJSON" scope="policy">
<arg-string>
<token-xpath expression="es:BuildQueryResults($appJSON, $readAttr)"/>
</arg-string>
</do-set-local-variable>
<do-strip-xpath expression="driver-operation-data/response/value"/>
<do-append-xml-element expression="driver-operation-data/response[last()]" name="value"/>
<do-append-xml-text expression="driver-operation-data/response/value[last()]">
<arg-string>
<token-local-variable name="queryJSON"/>
</arg-string>
</do-append-xml-text>
</actions>
</rule>



I'm not promising these are good code or account for all possibilities, but they are working for my use case...
0 Likes
tirthankar1984 Trusted Contributor.
Trusted Contributor.

Re: REST JSON format mismatch

Hi jrmhscht,
Thank you for you sample script and policy. I have tried it and not able to get the JSON in this time. Policy condition failed to do action.
Log: <if-xpath op="true">boolean(./request/value)</if-xpath> getting result "FALSE"
Also I need to understand this :
<do-set-local-variable name="netiqJSON" scope="policy">
<arg-string>
<token-xpath expression="../driver-operation-data/request/value/text()"/>
</arg-string>
</do-set-local-variable>

../driver-operation-data/request/value/text() ==> Is this the JSON data?
0 Likes
jrmhscht Super Contributor.
Super Contributor.

Re: REST JSON format mismatch

The condition (<if-xpath op="true">boolean(./request/value)</if-xpath>) is making sure there is JSON output from "NETQRESTJSON-otp-XDStoJSON". Here is a stripped version of what that output looks like:

<nds dtdversion="4.0" ndsversion="8.x">
<source>
<product edition="Advanced" version="4.6.1.0">DirXML</product>
<contact>NetIQ Corporation</contact>
</source>
<input>
<driver-operation-data class-name="User" command="modify" event-id="idv1#20180914191347#1#1:0ef7d90c-b59d-4d06-aab1-0cd9f70e9db5" src-dn="\IDVDEV\data\users\Dealers\N71893">
<request>
<url-token association="66d5110d-ebe6-4b3b-972f-e8de427148ed"/>
<header content-type="application/json"/>
<value>{ json output goes here}</value>
</request>
<operation-data check-entitlements="True">
</operation-data>
</driver-operation-data>
</input>
</nds>


The condition just checks to be sure that value exists. The xpath you listed is for the JSON output. The rule checks if it exists, converts it, and replaces it. It must be places directly after NETQRESTJSON-otp-XDStoJSON.
0 Likes
bhopal_smart Absent Member.
Absent Member.

Re: REST JSON format mismatch

In ECMAscript function like string replace, object declaration are not working, because when trying to save the script it will automatically delete the functions.
Add the ECMA script = https://ibb.co/mcHM3A
After saving the ECMA script = https://ibb.co/ghKjOA
0 Likes
Knowledge Partner
Knowledge Partner

Re: REST JSON format mismatch

bhopal smart wrote:

> object declaration are not
> working, because when trying to save the script it will automatically
> delete the functions.


That looks like an iManager bug, can you reproduce that with Designer? I've
never my ECMA stuff being altered when saving.

--
http://www.is4it.de/en/solution/identity-access-management/

(If you find this post helpful, please click on the star below.)
______________________________________________
https://www.is4it.de/identity-access-management
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.