Search code examples
javamuledataweaveflat-filemule4

JSON to flat file in Mule 4?


I have a simple requirement of converting input JSON to flat file in Mule 4 but I am unable to find any solid examples online. I started of creating sample schema as follows but it's not working.

test.ffd schema:

form: FLATFILE
id: 'test'
tag: '1'
name: Request Header Record
values:
- { name: 'aa', type: String, length: 10 }
- { name: 'bb', type: String, length: 8 }
- { name: 'cc', type: String, length: 4 }

dataweave:

%dw 2.0
output application/flatfile schemaPath='test.ffd'
---
{
    aa : payload.a,
    bb : payload.b,
    cc : payload.c
}

Input JSON:

{
  "a": "xxx",
  "b": "yyy",
  "c": "zzz"
}

But it fails saying

Message               : "java.lang.IllegalStateException - Need to specify structureIdent or schemaIdent in writer configuration, while writing FlatFile at 
4| {
 |  ...
8| }

How do I do this correctly?


Solution

  • Error message tells you what is missed.

    Need to specify structureIdent or schemaIdent in writer configuration

    Add one of them and it flatfile or fixedwidth should work fine.

    For example, add segmentIdent:

    %dw 2.0
    output application/flatfile schemaPath = "test1.ffd",
     segmentIdent = "test1"
    ---
    payload map (a, index) -> {
        aa: a.a,
        bb: a.b,
        cc: a.c
    }
    

    Good result with good preview

    Here is example how to use FIXEDWIDTH properly https://simpleflatservice.com/mule4/FixedWidthSchemaTransformation.html