When converting XML payloads to JSON and using "duplicateKeyAsArray=true" to convert duplicate key names to an array it works great. However, if there is only 1 element, the value will be returned as an object instead of an array with a single object. Is there a way to keep the field consistently returning an array?
My XML payload:
<?xml version='1.0' encoding='UTF-8'?>
<ns0:Project xmlns:ns0="http://xmlns.example.com/Product/">
<ns0:Products>
<ns0:RatingInfo>
<ns0:Rating>
<ns0:RatingMethod>TEST</ns0:RatingMethod>
<ns0:RatingDescriptors>
<ns0:RatingDescriptor>DESC1</ns0:RatingDescriptor>
<ns0:RatingDescriptor>DESC2</ns0:RatingDescriptor>
</ns0:RatingDescriptors>
</ns0:Rating>
</ns0:RatingInfo>
</ns0:Products>
</ns0:Project>
Yes, we can use the Dataweave update operator to update the single element into an array.
For example:
%dw 2.0
output application/json skipNullOn="everywhere"
var p0 = payload
var p1 = p0 update {
case descs at .*Project.*Products.*RatingInfo.*Rating.*RatingDescriptors -> {
RatingDescriptor: descs.*RatingDescriptor
}
}
var p2 = p1 update {
case rate at .*Project.*Products.*RatingInfo -> {
Rating: rate.*Rating
}
}
---
p2
001116066

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.