我有一个JSON对象
{
"collection": [
{
"field1": "1111",
"field2": "1122"
},
{
"field1": "2211",
"field2": "2222"
}
],
"otherObject": {
"otherField": "3333"
}
}
我想有这个输出:
S01&GT 1111 ~~~~~~ 1122 ~~~~~~
S01> 2211 ~~~~~~ 2222 ~~~~~~
S02> 3333 ~~~~~~
所以我使用了这种转变:
%dw 1.0
%output text/plain structureIdent = "response" , schemaPath = "response.ffd"
---
{
collection: payload.collection map ({
field1: $.field1,
field2: $.field2
}),
otherObject: {
otherField: payload.otherObject.otherField
}
}
和我的 response.ffd 就像这样
form: FIXEDWIDTH
structures:
- id: 'response'
name: response
tagStart: 0
data:
- { idRef: 'collection', count: '>1'}
- { idRef: 'otherObject', count: 1 }
segments:
- id: 'collection'
name: collection
tag: 's01>'
values:
- { name: 'field1', type: String, length: 10 }
- { name: 'field2', type: String, length: 10 }
- id: 'otherObject'
name: otherObject
tag: 's02>'
values:
- { name: 'otherField', type: String, length: 10 }
但是我得到了这个结果
s02>3333~~~~~~
好像 dataweave 不知道我的数组,我该怎么做才能让它工作?
答案 0 :(得分:2)
使用以下示例修改DataWeave代码:
[ ... map ...]
flatten
map
删除其他括号,因为collection: [{
field1: payload.collection.field1[0],
field2: payload.collection.field2[0]
},{
field1: payload.collection.field1[1],
field2: payload.collection.field2[1]
}]
本身已经返回数组虽然它返回相同的结果,但DataWeave以某种方式对待它的方式不同,就好像数据结构是手动编写的那样:例如:
{{1}}