使用dataweave将对象列表转换为csv [英] Transform list of objects into csv using dataweave
问题描述
我正在尝试使用dataweave中的以下代码将对象列表转换为csv:
I am trying to transform a list of objects to csv using the following code in dataweave:
%dw 1.0
%type company = :object {class: "java.util.ArrayList"}
%input payload application/java
%output application/csv
---
{
name: payload.name,
address: payload.address
} as :company
下面是执行上面的数据编织代码时得到的输出.
The below is the output that I get when I execute the above data weave code.
name,name
testName,testName2
testAddress,testAddress2
虽然我期望以下几点:(样本数据)
whilst I am expecting the following: (Sample data)
name,address
testName,testAddress
testName2,testAddress2
帮助我了解我在数据编织组件中缺少什么
Help me understand to what am I missing in the data weave component
推荐答案
一般来说,在使用DataWeave时,您使用规范表示来描述您的输出,规范表示或多或少是其他数据格式的超集.
In general terms, when using DataWeave you describe your output using a canonical representation which is more or less a super-set of other data formats.
要生成CSV输出,您需要生成一个对象数组.
这些对象均代表一个CSV行.
DataWeave中的对象是键值对的集合
To generate a CSV output you need to generate an array of objects.
Each of these objects represent a CSV row.
Objects in DataWeave are sets of key-value pairs
映射应类似于:
%dw 1.0
%output application/csv
---
payload map {
name: $.name,
address: $.address
}
此处的map
操作为列表中的每个条目生成一个具有name
和address
的对象. $
表示迭代(每个列表项)下的隐式变量.
The map
operation here generates an object with a name
and address
for each entry in the list. $
represents the implicit variable under iteration (each list entry).
注意:%input payload application/java
指令不是必需的,因为输入时,其内容类型(JSON,XML,CSV等)是从mule消息中获取的,如果不存在,则默认为java
Note: The %input payload application/java
directive is not necessary since the content-type for your input (JSON, XML, CSV, etc) is taken from the mule message when it is set, and it defaults to java if it's not present.
这篇关于使用dataweave将对象列表转换为csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!