'How to extract a nested array value frm XML in DataWeave and convert to CSV
I am getting this value from the database
<row>
<file_data>One</file_data>
<time_inserted>2019-01-30T10:29:20.543</time_inserted>
</row>
<row>
<file_data>two</file_data>
<time_inserted>2019-01-30T10:29:20.547</time_inserted>
</row>
<row>
<file_data>three</file_data>
<time_inserted>2019-01-30T10:29:20.550</time_inserted>
</row>
<row>
<file_data>four</file_data>
<time_inserted>2019-01-30T10:29:20.550</time_inserted>
</row>
<row>
<file_data>five</file_data>
<time_inserted>2019-01-30T10:29:20.553</time_inserted>
</row>
And I would like to output it as a CSV like this
one
two
three
four
five
Solution 1:[1]
I managed to solved my problem thusly:
Firstly I added a step in my logic to surround the data with a "rows" element so that it ended up looking like this:
<rows>
<row>
<file_data>One</file_data>
<time_inserted>2019-01-30T12:34:00.277</time_inserted>
</row>
<row>
<file_data>two</file_data>
<time_inserted>2019-01-30T12:34:00.277</time_inserted>
</row>
<row>
<file_data>three</file_data>
<time_inserted>2019-01-30T12:34:00.280</time_inserted>
</row>
<row>
<file_data>four</file_data>
<time_inserted>2019-01-30T12:34:00.280</time_inserted>
</row>
<row>
<file_data>five</file_data>
<time_inserted>2019-01-30T12:34:00.283</time_inserted>
</row>
</rows>
I then wrote this code to get the list output that I needed:
def responses = new XmlSlurper().parseText(xmlData)
def payload = ""
responses.row.findAll { p ->
p.file_data
}
.each { p ->
payload = payload + "${p.file_data}" + "\r\n"
}
print payload
And this outputs:
one
two
three
four
five
Solution 2:[2]
Hope below logic is works. please check it.
%dw 2.0
output application/java
---
payload.rows.*row map ($.file_data) reduce (($$ ++ "\n" ++ $))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Steven Mark Integration |
Solution 2 | Zoe stands with Ukraine |