XMLToJSON is throwing an error

I am using XMLTOJSON policy and it gives me below error.

{
"fault": {
"faultstring": "XMLToJSON[XML-to-JSON-1]: Execution failed. reason: Unexpected child node after root node",
"detail": {
"errorcode": "steps.xml2json.ExecutionFailed"
}
}
}

When I removed the XMLTOJSON policy I get below XML output

<?xml version="1.0" encoding="UTF-8"?>
<TRS>
<EMPLID>122</EMPLID>
<YEAR>2021</YEAR>
<MONTH>12</MONTH>
<BENEFIT_PREC>0</BENEFIT_PREC>
<AMOUNT>3</AMOUNT>
<CURRENCY_CD>USD</CURRENCY_CD>
</TRS>
<TRS>
<EMPLID>123</EMPLID>
<YEAR>2021</YEAR>
<MONTH>12</MONTH>
<BENEFIT_PREC>0</BENEFIT_PREC>
<AMOUNT>1</AMOUNT>
<CURRENCY_CD>USD</CURRENCY_CD>
</TRS>

XMLTOJSON policy I am using is

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XMLToJSON continueOnError="false" enabled="true" name="XML-to-JSON-1">
<DisplayName>XML to JSON-1</DisplayName>
<Properties/>
<Format>yahoo</Format>
<OutputVariable>response</OutputVariable>
<Source>response</Source>
</XMLToJSON>
1 3 367
3 REPLIES 3

Your XML is not a well-formed XML document.

According to the definition of XML, there must be exactly one root element. I have never been a fan of the wording used in the XML specifications. I find them to often be obtuse and confusingly phrased. But in this case it's mostly clear. An XML document must have exactly one root element.

root-element.png

If I reformat your XML with some indentation, it's easier to see the multiple root elements.

 

<TRS>
  <EMPLID>122</EMPLID>
  <YEAR>2021</YEAR>
  <MONTH>12</MONTH>
  <BENEFIT_PREC>0</BENEFIT_PREC>
  <AMOUNT>3</AMOUNT>
  <CURRENCY_CD>USD</CURRENCY_CD>
</TRS>
<TRS>
  <EMPLID>123</EMPLID>
  <YEAR>2021</YEAR>
  <MONTH>12</MONTH>
  <BENEFIT_PREC>0</BENEFIT_PREC>
  <AMOUNT>1</AMOUNT>
  <CURRENCY_CD>USD</CURRENCY_CD>
</TRS>

 

That indentation makes it easier to see the two TRS elements at the root. The spec says there can be only one. Therefore XMLToJSON is rightly throwing an exception, telling you, "Hey this isn't well-formed XML." If you wrap that XML fragment within a single root element, like this:

 

<ANY_NAME_HERE>
  <TRS>
    <EMPLID>122</EMPLID>
    <YEAR>2021</YEAR>
    <MONTH>12</MONTH>
    <BENEFIT_PREC>0</BENEFIT_PREC>
    <AMOUNT>3</AMOUNT>
    <CURRENCY_CD>USD</CURRENCY_CD>
  </TRS>
  <TRS>
    <EMPLID>123</EMPLID>
    <YEAR>2021</YEAR>
    <MONTH>12</MONTH>
    <BENEFIT_PREC>0</BENEFIT_PREC>
    <AMOUNT>1</AMOUNT>
    <CURRENCY_CD>USD</CURRENCY_CD>
  </TRS>
</ANY_NAME_HERE>

 

...then it will be "well formed", and the XMLToJSON policy will be able to process it.

Where'd you get that XML anyway?

The XML I generated was using OXYGEN. I have written XSLT transformation which gave me this output.

After adding one root element it works.

However, is there a way to remove this root element from JSON output when I convert XMLtoJSON. Expected JSON is

 

{
{
 "EMPLID" : "122",
  "YEAR"    : "2021",
  "MONTH" : "12"
},
{
 "EMPLID" : "123",
  "YEAR"    : "2021",
  "MONTH" : "12"
}
}

 

What you showed is not valid JSON.

And....  you cannot persuade the XMLToJSON policy to produce that.