How to use DataWeave to extract a subtable in an Excel Spreadsheet?
For example, we have an Example.xlsx in which on the Trans sheet there is a train list from row 6 to row 21, we would like to fetch this list and generate a JSON payload.
%dw 2.0
output application/json
---
readUrl("classpath://Example.xlsx","application/xlsx")
<ee:transform doc:name="Transform Message" doc:id="5acaac2a-4060-4460-a3a8-1ad190f5bb6e" >
<ee:message >
<ee:set-payload ><![CDATA[
%dw 2.0
output application/json
var allRecords = (payload pluck $)[0]
fun isTitle (obj: Object) = ! isEmpty(((obj pluck $) map ($ matches /.*Train Name.*/)) filter $)
var title = (allRecords filter (item, index) -> (isTitle(item)))[0]
var titleFields = (title pluck $) map ($ replace /[^0-9a-zA-Z:\s]/ with "")
var idx = (allRecords find title)[0]
var restOfRecords = allRecords[idx+1 to -1]
fun addTitle (obj: Object) = (obj mapObject (value,key,index) -> { (titleFields[index]) : (value) }) -- { "": ""}
var formatedRecords = restOfRecords map (addTitle($))
---
formatedRecords
]]>
</ee:set-payload>
</ee:message>
</ee:transform>
001117066

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.