DataPower

DataPower

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  Conversion from JSON to BadgerFish XML

    Posted Mon November 18, 2019 09:52 AM
    I am facing an issue while converting JSON to BadgerFish form. Error as below. I know something is wrong is for loop. 

    Any help in this is highly appreciated


    "errorMessage": "Unable to convert JSON in 'badgerfish' format to XML",
    "errorCode": "0x85800092",
    "errorDescription": "Unable to convert JSON to XML. The input is not in the specified format.",

    Code Snippet


    let RepaymentAmount = {}
    for (let el in reqBody.LoanRepaymentDetails) {
    RepaymentAmount[el] = {$:reqBody.LoanRepaymentDetails[el]}
    }

    let bfReqBody = {
    LoanPmtCalcInqRq: {
    'LoanRepaymentDetails':{
    RepaymentAmount,
    },
    },
    }
    let xmlReqBody = converter.toXML('badgerfish', bfReqBody)


    Input JSON

    {{ "LoanRepaymentDetails": [{ "PaymentType": "com.anz.InterestPayment", "PmtTimeframe": { "RecurrenceRule": { "RecurrenceType": "MAR" } } }, { "PaymentType": "com.anz.PrincipalPayment", "PmtTimeframe": { "RecurrenceRule": { "RecurrenceType": "Monthly" } } }]}

    Desired XML

    <LoanPmtCalcInqRq><LoanPmtCalcInqRq><LoanRepaymentDetails> <RepaymentAmount> <PaymentType>com.anz.InterestPayment</PaymentType> <PmtTimeframe> <RecurrenceRule> <RecurrenceType>MAR</RecurrenceType> </RecurrenceRule> </PmtTimeframe> </RepaymentAmount> <RepaymentAmount> <PaymentType>com.anz.PrincipalPayment</PaymentType> <PmtTimeframe> <RecurrenceRule> <RecurrenceType>Monthly</RecurrenceType> </RecurrenceRule> </PmtTimeframe> </RepaymentAmount> </LoanRepaymentDetails></LoanPmtCalcInqRq>

    Desired BadgerFish

    "LoanPmtCalcInqRq":{"LoanRepaymentDetails":{"RepaymentAmount":{"PaymentType":{"$":{"com.anz.InterestPayment"}}
    "PmtTimeframe":{"RecurrenceRule":{"RecurrenceType":{$:"MAR"}}}},
    "RepaymentAmount":{"PaymentType":{"$":{"com.anz.PrincipalPayment"}}
    "PmtTimeframe":{"RecurrenceRule":{"RecurrenceType":{$:"Monthly"}}}}}





    ------------------------------
    Varun Rao
    ------------------------------


  • 2.  RE: Conversion from JSON to BadgerFish XML

    Posted Wed November 20, 2019 05:39 AM
    Hi,
    your loop doesn't produce valid BadgerFish. Your desired BadgerFish should look something like the following:

    {
    	"LoanPmtCalcInqRq": {
    		"LoanRepaymentDetails": {
    			"RepaymentAmount": [{
    				"PaymentType": {
    					"$": "com.anz.InterestPayment"
    				},
    				"PmtTimeframe": {
    					"RecurrenceRule": {
    						"RecurrenceType": {
    							"$": "MAR"
    						}
    					}
    				}
    			}, {
    				"PaymentType": {
    					"$": "com.anz.PrincipalPayment"
    				},
    				"PmtTimeframe": {
    					"RecurrenceRule": {
    						"RecurrenceType": {
    							"$": "Monthly"
    						}
    					}
    				}
    			}]
    		}
    	}
    }


    ------------------------------
    Hermanni Pernaa
    ------------------------------



  • 3.  RE: Conversion from JSON to BadgerFish XML

    Posted Thu November 21, 2019 07:25 AM
    I hope this is something that you can use. I'm not a Javascript developer, so I apologize if the code is not according to best practices. This is just to give you an idea what might work.
    //Set the required libraries
    var converter = require('json-xml-converter');
    var hm = require('header-metadata');
    
    //Function for traversing through JSON tree
    function parseBF(obj) {
    	//Temporary object for storing traverse results
    	var tmpObj = {};
    	
    	//Process array objects
    	if (Array.isArray(obj)) {
    		var arr = [];
    		
    		//traverse through array content
    		for ( var i in obj) {
    			if (obj[i] !== null && typeof (obj[i]) == "object") {
    				arr.push(parseBF(obj[i]));
    			} else {
    				arr.push({$ : obj[i]});
    			}
    		}
    		tmpObj = arr;
    		
    	} 
    	//IF not array, process other objects
    	else {
    		for ( var i in obj) {
    			if (obj[i] !== null && typeof (obj[i]) == "object") {
    				tmpObj[i] = parseBF(obj[i]);
    			} else {
    				//If not array or object, assume that we have string, etc. value
    				tmpObj[i] = {
    					$ : obj[i]
    				};
    			}
    		}
    	}
    	return tmpObj;
    }
    
    //Parse incoming JSON payload
    session.input
    		.readAsJSON(function(error, reqBody) {
    			if (error) {throw error;}
    			else {
    				
    				//Create BadgerFish JSON from request payload
    				let bfjson = parseBF(reqBody);
    				
    				//Create the new request body with the parsed BadgerFish content
    				let bfReqBody = {LoanPmtCalcInqRq: {LoanRepaymentDetails: {RepaymentAmount: bfjson.LoanRepaymentDetails}}};
    				
    				//Log entry for debug purposes
    				console.log(JSON.stringify(bfReqBody));
    				
    				//Convert BadgerFish JSON to XML
    				let xmlReqBody = converter.toXML('badgerfish', bfReqBody);
    				
    				//Set content type to XML
    				hm.current.set("Content-Type", "text/xml");
    				
    				//Send XML request body to output
    				session.output.write(xmlReqBody);
    			}
    		});

    The code above produces the following output with the given input:
    INPUT JSON:

    {
    	"LoanRepaymentDetails": [{
    		"PaymentType": "com.anz.InterestPayment",
    		"PmtTimeframe": {
    			"RecurrenceRule": {
    				"RecurrenceType": "MAR"
    			}
    		}
    	}, {
    		"PaymentType": "com.anz.PrincipalPayment",
    		"PmtTimeframe": {
    			"RecurrenceRule": {
    				"RecurrenceType": "Monthly"
    			}
    		}
    	}]
    }

    OUTPUT XML:

    <LoanPmtCalcInqRq>
       <LoanRepaymentDetails>
          <RepaymentAmount>
             <PaymentType>com.anz.InterestPayment</PaymentType>
             <PmtTimeframe>
                <RecurrenceRule>
                   <RecurrenceType>MAR</RecurrenceType>
                </RecurrenceRule>
             </PmtTimeframe>
          </RepaymentAmount>
          <RepaymentAmount>
             <PaymentType>com.anz.PrincipalPayment</PaymentType>
             <PmtTimeframe>
                <RecurrenceRule>
                   <RecurrenceType>Monthly</RecurrenceType>
                </RecurrenceRule>
             </PmtTimeframe>
          </RepaymentAmount>
       </LoanRepaymentDetails>
    </LoanPmtCalcInqRq>
    Hope you get it working.

    ------------------------------
    Hermanni Pernaa
    ------------------------------