如何将复杂对象映射到 DataWeave 固定宽度格式
How do I map a complex object to DataWeave fixed width format
我有一个JSON对象
{
"collection": [
{
"field1": "1111",
"field2": "1122"
},
{
"field1": "2211",
"field2": "2222"
}
],
"otherObject": {
"otherField": "3333"
}
}
我想要这样的输出:
s01>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 不知道我的数组,我应该怎么做才能让它工作?
使用以下示例修改 DataWeave 代码:
%dw 1.0
%output text/plain structureIdent = "response" , schemaPath = "response.ffd"
---
{
collection: flatten [payload.collection map ({
field1: $.field1,
field2: $.field2
})],
otherObject: {
otherField: payload.otherObject.otherField
}
}
- 通过添加括号确保集合是数组:
[ ... map ...]
- 使用
flatten
删除附加括号,因为 map
本身已经 return 一个数组
虽然它 return 是相同的结果,但 DataWeave 以某种方式对待它的方式不同,就好像数据结构是手动编写的一样:例如:
collection: [{
field1: payload.collection.field1[0],
field2: payload.collection.field2[0]
},{
field1: payload.collection.field1[1],
field2: payload.collection.field2[1]
}]
我有一个JSON对象
{
"collection": [
{
"field1": "1111",
"field2": "1122"
},
{
"field1": "2211",
"field2": "2222"
}
],
"otherObject": {
"otherField": "3333"
}
}
我想要这样的输出:
s01>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 不知道我的数组,我应该怎么做才能让它工作?
使用以下示例修改 DataWeave 代码:
%dw 1.0
%output text/plain structureIdent = "response" , schemaPath = "response.ffd"
---
{
collection: flatten [payload.collection map ({
field1: $.field1,
field2: $.field2
})],
otherObject: {
otherField: payload.otherObject.otherField
}
}
- 通过添加括号确保集合是数组:
[ ... map ...]
- 使用
flatten
删除附加括号,因为map
本身已经 return 一个数组
虽然它 return 是相同的结果,但 DataWeave 以某种方式对待它的方式不同,就好像数据结构是手动编写的一样:例如:
collection: [{
field1: payload.collection.field1[0],
field2: payload.collection.field2[0]
},{
field1: payload.collection.field1[1],
field2: payload.collection.field2[1]
}]