如何将复杂对象映射到DataWeave固定宽度格式

时间:2016-08-04 13:43:19

标签: mule fixed-width anypoint-studio dataweave

我有一个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 不知道我的数组,我该怎么做才能让它工作?

1 个答案:

答案 0 :(得分:2)

使用以下示例修改DataWeave代码:

[ ... map ...]
  1. 通过添加括号flatten
  2. 确保集合是数组
  3. 使用map删除其他括号,因为collection: [{ field1: payload.collection.field1[0], field2: payload.collection.field2[0] },{ field1: payload.collection.field1[1], field2: payload.collection.field2[1] }] 本身已经返回数组
  4. 虽然它返回相同的结果,但DataWeave以某种方式对待它的方式不同,就好像数据结构是手动编写的那样:例如:

    {{1}}