I am using XmltoJson policy and trying to use TreatAsArray option( to treat an element with single occurrence also as an array), but as i add this option i am getting following error:
Error occurred while validation of bean XML-to-JSON-1.xml. Reason: - Schema validation failed. Cause : unexpected element (uri:"", local:"TreatAsArray"). Expected elements are <{}NamespaceBlockName>,<{}OutputPrefix>,<{}TextAlwaysAsProperty>,<{}NullValue>,<{}OutputSuffix>,<{}AttributeBlockName>,<{}RecognizeBoolean>,<{}AttributePrefix>,<{}TextNodeName>,<{}DefaultNamespaceNodeName>,<{}NamespaceSeparator>,<{}RecognizeNull>,<{}RecognizeNumber>. Line number : 9. Column number : 23. File name : XML-to-JSON-1.xml..
The policy attached and error while saving is :
The error remains same even if i add all the options.
If anyone can help me , it would be very helpful.
Regards,
Alok
Are you still seeing this error? I created/saved/tested the same basic policy successfully. I can't see any problem with the one you shared. Here's my policy:
<XMLToJSON async="false" continueOnError="false" enabled="true" name="XML-to-JSON"> <DisplayName>XML to JSON</DisplayName> <Properties/> <Options> <RecognizeNull>true</RecognizeNull> <TextNodeName>#text</TextNodeName> <AttributePrefix>@</AttributePrefix> <TreatAsArray> <Path unwrap="true">Base/Item/name</Path> </TreatAsArray> </Options> <OutputVariable>response</OutputVariable> <Source>response</Source> </XMLToJSON>
This is the data I converted:
<Base> <Item> <name>pod1</name> <region>us-east-1</region> </Item> <Item> <name>pod2</name> <region>us-west-2</region> </Item> </Base>
And the response -- where the "name" attribute is returned as an array:
{ "Base": { "Item": [{ "name": ["pod1"], "region": "us-east-1" }, { "name": ["pod2"], "region": "us-west-2" }] } }<br>
Just to be helpful, I put together a quick illustration of XMLToJSON with TreatAsArray . This is available as a deployable API proxy bundle, with some scripts that help you deploy and run it. All in one github repo.
And, I did a quick screencast on the topic, too. Here 'tis:
Thanks @Dino. Is the option 'TreatAsArray' available in Private Cloud? If Not, Is there any workaround to achieve this in Private Cloud?
@Alok Kumar Singh, are you using Private Cloud? If so, that element isn't available yet.
I didn't realize TreatAsArray is only in Public Cloud at this time. You can of course ignore my answer below if you are using Private Cloud.