Transform fixed width input to JSON

Example based on:

Incoming message body:

123ABC210210

456DEF210211

789GHI210212

Integration flow output:

Three separate JSONs (split is configurable in the flatpack component config parameters; default value is true)

{"date":"210210","col2":"ABC","col1":"123"}

{"date":"210211","col2":"DEF","col1":"456"}

{"date":"210212","col2":"GHI","col1":"789"}

Used components:

flatpack

JSON Jackson

Dependencies (in pom.xml)

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-flatpack</artifactId>
  <version>x.x.x</version><!-- use the same version as your Camel core version -->
</dependency>
<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-jackson</artifactId>
  <version>x.x.x</version><!-- use the same version as your Camel core version -->
</dependency>

parser.pzmap.xml file

  1. Create a parser.pzmap.xml file with content:
<?xml version='1.0'?>
<!DOCTYPE PZMAP SYSTEM "flatpack.dtd" >
<PZMAP>
    <COLUMN name="col1" length="3" />
    <COLUMN name="col2" length="3" />
    <COLUMN name="date" length="6" />
</PZMAP>
  1. File must be included in the Integration Flow package e.g. ik-flow-release/parser.pzmap.xml

Integration Flow definition:

    <route>
      <from id="flatpack" uri="direct:start"/>
      <to uri="flatpack:fixed:ik-flow-release/parser.pzmap.xml"/> <!--flatpack:<type>:<resourceUri - directory of pzmap file> -->
    </route>

    <route>
      <from uri="flatpack:fixed:ik-flow-release/parser.pzmap.xml"/> <!--access to the transformed data-->
      <to uri="log:xy"/>
      <convertBodyTo type="java.util.Map"/>
      <log id="_log1" message="${body}"/>
      <marshal id="_marshal1">
        <custom ref="json-jackson"/>
      </marshal>
      <convertBodyTo id="deserialize-returned-byte-array" type="java.lang.String"/>
      <to uri="mock:results"/>
    </route>

Results in the Integration Kit flow history:

Incoming message: image.png

Transformation with flatpack: image.png

Converting the exchange to map: image.png

Converting map to JSON with JSON Jackson image.png

Deserialized byte array: image.png