sivaramtm Super Contributor.
Super Contributor.

Re: Help with parsing rest json response to XDS

Jump to solution
any suggestion on how to do this conversion.
0 Likes
Highlighted
sivaramtm Super Contributor.
Super Contributor.

Re: Help with parsing rest json response to XDS

Jump to solution
Input tags are getting add by default from the engine. How do we change them to output?
0 Likes
Marcus Tornberg Super Contributor.
Super Contributor.

Re: Help with parsing rest json response to XDS

Jump to solution

Hi!

I do not think the input/output is your real problem at this point.

I would guess if you want to send the event through schema mapping and filters, you would want an add or modify, not an instance. Not sure if you have described what the overall goal is? I could not find it, but the thread is getting quite large.

To make it as an add or modify (I would do an add event) you need to adjust the solution provided by Klasen to result in an add/modify and not an instance.

Best regards

Marcus

0 Likes
Knowledge Partner
Knowledge Partner

Re: Help with parsing rest json response to XDS

Jump to solution

If I understand your use case properly, you sent a query on the Sub channel to the REST endpoint.

 

So you sent an <input> document.

The response should have been from the shim as an <output> document.  Your <instance> should have been set under the existing <output> node.  Maybe Norberts approach needs some minor tweaking.

sivaramtm Super Contributor.
Super Contributor.

Re: Help with parsing rest json response to XDS

Jump to solution
Sorry for the confusion. My use case is sending the query in publisher poll using Get method and trying to synchronize the values into IDV.
0 Likes
Marcus Tornberg Super Contributor.
Super Contributor.

Re: Help with parsing rest json response to XDS

Jump to solution

Hi.

I have updated Klasens ECMA-script to return an add event instead of an instance the way I would have done it. Try it and let us know.

/**
 * parse JSON returned by REST service and transform it into XDS elements
 * @Param {string} payload JSON encoded string
 * @return {NodeSet} input converted to a NodeSet
 */
function JSONtoXDS (payload) {
    var nodeSet = new Packages.com.novell.xml.xpath.NodeSet();
    var document = Packages.com.novell.xml.dom.DocumentFactory.newDocument();
    var statusElement = document.createElement('status');

    try {
        var response = JSON.parse(payload);
    } catch (e) {
        // parsing error
        statusElement.setAttributeNS(null, 'level', 'error');
        statusElement.setAttributeNS(null, 'type', 'driver-general');
        statusElement.appendChild(document.createTextNode(e.toString()));
        statusElement.appendChild(document.createTextNode(payload));
        nodeSet.add(statusElement);
        return (nodeSet);
    }

    if (Array.isArray(response.result)) {
        // create the add node
        for (var i = 0; i < response.result.length; i++) {
            var instanceElement = document.createElement('add');
            instanceElement.setAttributeNS(null, 'class-name', 'User');
            var associationElement = document.createElement('association');
            associationElement.appendChild(document.createTextNode(response.result[i]['u_kundenr']));
            instanceElement.appendChild(associationElement);
            for (var key in response.result[i]) {
                var attrElement = document.createElement('add-attr');
                attrElement.setAttributeNS(null, 'attr-name', key);
                var valueElement = document.createElement('value');
                var value = response.result[i][key];
                if (typeof value !== 'string') {
                    value = JSON.stringify(value);
                }
                valueElement.appendChild(document.createTextNode(value));
                attrElement.appendChild(valueElement);
                instanceElement.appendChild(attrElement);
            }
            nodeSet.add(instanceElement);
        }
    } else {
        // handle application specific error
        statusElement.setAttributeNS(null, 'level', 'error');
        statusElement.setAttributeNS(null, 'type', 'app-general');
        statusElement.appendChild(document.createTextNode('some error'));
        nodeSet.add(statusElement);
    }
    return (nodeSet);
}

 Note that I have hard-coded "User" as class-name and the association value comes from  u_kundenr.

sivaramtm Super Contributor.
Super Contributor.

Re: Help with parsing rest json response to XDS

Jump to solution
How to parse when multiple values returned in same response.
{"result":[{"parent":"","total_cost":"kr 0,00","discount":"","cost_adjustment":"0","sys_updated_on":"06.12.2018 12:52:02","u_response_agreement":"","u_sapkontraktnr":"40069112","u_external_problems_allowed":"false","approval_history":"","number":"CNTR0374773","sub_total_cost":"kr 0,00","u_solution_architect":"","cost_adjustment_reason":"","state":"Active","sys_created_by":"RestMQUser","contract_composite":"40069112 - CNTR0374773 - 218199","yearly_cost":"kr 0,00","active":"true","payment_amount":"kr 0,00","u_language":"Danish","sys_domain_path":"!!!/!!#/!!$/","u_idm_2nd_level_approval":"false","terms_and_conditions":null,"u_problem_internal_priority":"5","cost_center":"","renewable":"true","contract_administrator":"","template":"","short_description":"Frederiksberg Forsyning A/S -","process_non_contractual_slas":"false","u_expected_resolution_email":"false","sys_class_name":"Contract","u_first_time_fix_allowed":"false","u_sdms":"","vendor":"","contract_model":"","u_contract_reference":"Purchase Agreement","approver":"","monthly_cost":"kr 0,00","license_type":null,"renewal_options":null,"substate":"Awaiting Review","application_model":"","u_problem_external_priority":"3","vendor_account":"","u_business_hours":"","cost_adjustment_percentage":"0","u_idm_2nd_level_approvers":"","renewal_end_date":"","tax_rate":"","tax_cost":"kr 0,00","sys_updated_by":"system","u_cab_approval_group":"","renewal_date":"","u_head_of_delivery":"","u_security_condition":"","sys_created_on":"06.10.2016 17:32:16","sys_domain":{"display_value":"TOP/MSP/BBC","link":"http://servicenow-test.BBC.dk/api/now/table/domain/4c346138a44f12005821241c95d66248"},"u_problem_coordinator":"","parent_contract":"","starts":"","tax_exempt":"false","commitment":"kr 0,00","payment_schedule":"Monthly","renewal_contact":"","expiration":"","invoice_payment_terms":null,"u_ci_access_group":"","u_department":"","u_send_survey_along_with_auto_resolve_mail":"false","ratecard":"false","cost_adjustment_type":null,"u_ci_visibility_usage_group":"","u_notes_ok_for_portal":"false","description":"","u_incident_priority":"3","u_reopen_time":"5","sys_id":"00007fadff5e220051b26dff7fa4ad59","po_number":"","license_quantity_entitled":"","u_approver":"","ends":"","consumer":"","process":null,"vendor_contract":"218199","lifetime_cost":"kr 0,00","sys_mod_count":"5","u_service_request_priority":"5","sys_tags":"","u_automatic_closure_mail_is_being_sent":"false","u_risk_assesment_group":"","u_head_of_business":"","sales_tax":"true","cost_per_unit":"kr 0,00","location":"","u_service_delivery_manager":"","account":"","u_delivery_manager":""},{"parent":"","total_cost":"kr 0,00","discount":"","cost_adjustment":"0","sys_updated_on":"14.03.2019 12:31:25","u_response_agreement":"","u_sapkontraktnr":"40026088","u_external_problems_allowed":"false","approval_history":"","number":"CNTR0328059","sub_total_cost":"kr 0,00","u_solution_architect":"","cost_adjustment_reason":"","state":"Active","sys_created_by":"RestMQUser","contract_composite":"40026088 - CNTR0328059 - 170358","yearly_cost":"kr 0,00","active":"true","payment_amount":"kr 0,00","u_language":"Danish","sys_domain_path":"!!!/!!#/!!$/","u_idm_2nd_level_approval":"false","terms_and_conditions":null,"u_problem_internal_priority":"5","cost_center":"","renewable":"true","contract_administrator":"","template":"","short_description":"Danske Diakonhjem -","process_non_contractual_slas":"false","u_expected_resolution_email":"false","sys_class_name":"Contract","u_first_time_fix_allowed":"false","u_sdms":"","vendor":"","contract_model":"","u_contract_reference":"Purchase Agreement","approver":"","monthly_cost":"kr 0,00","license_type":null,"renewal_options":null,"substate":"Awaiting Review","application_model":"","u_problem_external_priority":"3","vendor_account":"","u_business_hours":"","cost_adjustment_percentage":"0","u_idm_2nd_level_approvers":"","renewal_end_date":"","tax_rate":"","tax_cost":"kr 0,00","sys_updated_by":"adminKUR","u_cab_approval_group":"","renewal_date":"","u_head_of_delivery":"","u_security_condition":"","sys_created_on":"06.10.2016 16:22:22","sys_domain":{"display_value":"TOP/MSP/BBC","link":"http://servicenow-test.BBC.dk/api/now/table/domain/4c346138a44f12005821241c95d66248"},"u_problem_coordinator":"","parent_contract":"","starts":"","tax_exempt":"false","commitment":"kr 0,00","payment_schedule":"Monthly","renewal_contact":"","expiration":"","invoice_payment_terms":null,"u_ci_access_group":"","u_department":"","u_send_survey_along_with_auto_resolve_mail":"false","ratecard":"false","cost_adjustment_type":null,"u_ci_visibility_usage_group":"","u_notes_ok_for_portal":"false","description":"","u_incident_priority":"3","u_reopen_time":"5","sys_id":"0000a761ff5e220051b26dff7fa4ad78","po_number":"","license_quantity_entitled":"","u_approver":"","ends":"","consumer":"","process":null,"vendor_contract":"170358","lifetime_cost":"kr 0,00","sys_mod_count":"6","u_service_request_priority":"5","sys_tags":"","u_automatic_closure_mail_is_being_sent":"false","u_risk_assesment_group":"","u_head_of_business":"","sales_tax":"true","cost_per_unit":"kr 0,00","location":"","u_service_delivery_manager":"","account":"","u_delivery_manager":""}]}
0 Likes
sivaramtm Super Contributor.
Super Contributor.

Re: Help with parsing rest json response to XDS

Jump to solution
Thank you so much for the updated code now synchronized values from target to IDV successfully.
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.