简体   繁体   中英

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?

Assuming you are trying to output a fixed width file, which it looks like you are, change

form: FLATFILE

to

form: FIXEDWIDTH

Keep in mind using this FFD will only work if you have a single structure. You could pass in:

    payload map {
        aa: $.a,
        ...
    }

If you had a set and it would still work, but if you needed multiple structures you won't be able to use the shorthand schema.

And to explain why you were getting this error, take a look at these docs, reading "Writer properties (for Flat File)":

https://docs.mulesoft.com/mule-runtime/4.2/dataweave-formats#writer_properties_flat_file

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
}

良好的预览效果

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM