XMLToJSON policy: Does TreatAsArray work in Evaluation version?

Not applicable

I am trying out to check the TreatAsArray option of XMLToJson, but its not working for an array of one item. If multiple items with or without this option, it's working as expected.

Given the policy details below., can someone help with what;s going wrong here?

<XMLToJSON name="XML-to-JSON-1"> 
  <DisplayName>XML to JSON-1</DisplayName> 
  <Properties/> 
  <Options> 
    <RecognizeNull>true</RecognizeNull> 
    <TextNodeName>#text</TextNodeName> 
    <AttributePrefix>@</AttributePrefix> 
    <TreatAsArray> 
      <!-- new element --> 
      <Path unwrap="true">/ACORD/InsuranceSvcRq/ManagementLiabilityPolicyQuoteInqRq/CrimeLineBusiness/QuestionAnswer</Path> 
    </TreatAsArray> 
  </Options> 
  <!--<Format>yahoo</Format>--> 
  <OutputVariable>jsonrequest</OutputVariable> 
  <Source>request</Source> 
</XMLToJSON>

Please note I am using an evaluation version of Apigee Edge; not sure if the policy is limited here. Below is the build info of my evaluation version.

SCM_REVISION=0a566852b4abc5b671f55713f6e1fc4f408b99c9
SCM_BRANCH=
BUILD_TIMESTAMP=1541612060859
BUILD_NUMBER=local
RELEASE_ID=181105
RPM_NAME=enterpriseUI-181105-0a566852-20181107-093300.tgz
Solved Solved
0 6 155
1 ACCEPTED SOLUTION

Can you try this: Eliminate the leading slash in the xpath?

<XMLToJSON name='XMLToJSON-1'>
  <Source>request</Source>
  <OutputVariable>transformed.content</OutputVariable>
  <Options>
    <TreatAsArray>
      <!-- unwrap=true means omit the element -->
      <Path unwrap='true'>ACORD/InsuranceSvcRq/ManagementLiabilityPolicyQuoteInqRq/CrimeLineBusiness/QuestionAnswer</Path>
    </TreatAsArray>
  </Options>
</XMLToJSON>

When I try this , I get this output:

{
  "ACORD": {
    "InsuranceSvcRq": {
      "RqUID": "3F16ACBE-2B1D-4304-8639-67EFA3C0AC74",
      "ManagementLiabilityPolicyQuoteInqRq": {
        "CurCd": "USD",
        "CrimeLineBusiness": [
          {
            "QuestionCd": "FIDUC02",
            "YesNoCd": "YES",
            "Explanation": "This is an explanation of in-house investment decisions"
          }
        ]
      }
    }
  }
}

As you can see the QuestionAnswer element has been treated as an array, and because you used unwrap='true', that element does not appear in the output JSON.

View solution in original post

6 REPLIES 6

Not applicable

Formatted XML

<XMLToJSON async="false" continueOnError="false" enabled="true" name="XML-to-JSON-1">
    <DisplayName>XML to JSON-1</DisplayName>
    <Properties/>
    <Options>
        <RecognizeNull>true</RecognizeNull>
        <TextNodeName>#text</TextNodeName>
        <AttributePrefix>@</AttributePrefix>
        <TreatAsArray>
            <!-- new element -->
           <Path unwrap="true"><strong><em>/ACORD/InsuranceSvcRq/ManagementLiabilityPolicyQuoteInqRq/CrimeLineBusiness/QuestionAnswer</em></strong></Path>
        </TreatAsArray>
    </Options>
    <!--<Format>yahoo</Format>-->
    <OutputVariable>jsonrequest</OutputVariable>
    <Source>request</Source>
</XMLToJSON>

Hi Anwar,

Yes, the TreatAsArray works in all evaluation versions of Apigee Edge.

I can help explain what's happening. Can you show me an example XML payload that you send through this policy?

Hi Dino,

Please find the below sample xml i have tried for the test

<?xml version="1.0" encoding="UTF-8"?>
<ACORD>
<InsuranceSvcRq>
<RqUID>3F16ACBE-2B1D-4304-8639-67EFA3C0AC74</RqUID>
<ManagementLiabilityPolicyQuoteInqRq>
<CurCd>USD</CurCd>
<!-- DO Start-->
<!-- DO END-->
<!-- EPL Start-->
<!-- EPL End-->
<!-- Crime and Fiduciary-->
<CrimeLineBusiness>
<!--VI. CRIME COVERAGE INFORMATION             1. Number of U.S. Locations-->
<QuestionAnswer>
<QuestionCd>FIDUC02</QuestionCd>
<YesNoCd>YES</YesNoCd>
<!--V. FIDUCIARY LIABILITY COVERAGE INFORMATION 1. Plan Information: (b) Does the Applicant handle any investment decisions in-house? -->
<Explanation>This is an explanation of in-house investment decisions</Explanation>
<!--V. FIDUCIARY LIABILITY COVERAGE INFORMATION 1. Plan Information: (b) Does the Applicant handle any investment decisions in-house? If Yes, please describe -->
</QuestionAnswer>
</CrimeLineBusiness>
</ManagementLiabilityPolicyQuoteInqRq>
</InsuranceSvcRq>
</ACORD>

Below is the JSON

{
	"ACORD": {
		"InsuranceSvcRq": {
			"RqUID": "3F16ACBE-2B1D-4304-8639-67EFA3C0AC74",
			"ManagementLiabilityPolicyQuoteInqRq": {
				"CurCd": "USD",
				"CrimeLineBusiness": {
					"QuestionAnswer": {
						"QuestionCd": "FIDUC02",
						"YesNoCd": "YES",
						"Explanation": "This is an explanation of in-house investment decisions"
					}
				}
			}
		}
	}
}

Thank you for your help!!

Thanks, that helps. OK I've posted my answer below. Short version: omit the leading slash in the path.

Can you try this: Eliminate the leading slash in the xpath?

<XMLToJSON name='XMLToJSON-1'>
  <Source>request</Source>
  <OutputVariable>transformed.content</OutputVariable>
  <Options>
    <TreatAsArray>
      <!-- unwrap=true means omit the element -->
      <Path unwrap='true'>ACORD/InsuranceSvcRq/ManagementLiabilityPolicyQuoteInqRq/CrimeLineBusiness/QuestionAnswer</Path>
    </TreatAsArray>
  </Options>
</XMLToJSON>

When I try this , I get this output:

{
  "ACORD": {
    "InsuranceSvcRq": {
      "RqUID": "3F16ACBE-2B1D-4304-8639-67EFA3C0AC74",
      "ManagementLiabilityPolicyQuoteInqRq": {
        "CurCd": "USD",
        "CrimeLineBusiness": [
          {
            "QuestionCd": "FIDUC02",
            "YesNoCd": "YES",
            "Explanation": "This is an explanation of in-house investment decisions"
          }
        ]
      }
    }
  }
}

As you can see the QuestionAnswer element has been treated as an array, and because you used unwrap='true', that element does not appear in the output JSON.

Thank you !! I missed to notice that.