Simple flat file parsing with the webMethods flat file adapter

Introduction

In the article I will describe how to create a flat file schema from a given file and use it to parse the fie and convert it to json or xml. I will also show how to use a ftp port to send and receive the files.

The complete sample code can be found here on Github.

Requirements

webMethods Designer and an Integration Server or Microservice runtime >= 10.5 or just webMethods Service Designer. You can get Service Designer from this page in our TechCommunity. If you need guidelines how to get it running, please have a look at the first section of my article Build a microservice converting csv flat file to xml or json using Service Designer.

The flat file I use is oriented on the bank statement of a bank:

Auftragskonto;Buchungstag;Valutadatum;Buchungstext;Verwendungszweck;Beguenstigter/Zahlungspflichtiger;Kontonummer;BLZ;Betrag;Waehrung;Info
DE97100100000000123456;01.10.2019;03.10.2019;debit note;Birdseed;ACME;DE85200100000000654321;20010000;300,00;EUR;
DE97100100000000123456;02.10.2019;04.10.2019;debit note;Springs;EMCA;DE17300300000000333555;30030000;500,95;EUR;

Just import the finished example

If youjust want to wath the code working, you can get the ready made sample code from Github here. If you want to build the example (or get a desciption of the code), please see below in Build the solution.

If you want to get the package properly into your workspace using the git integration of designer, please have a look at my article about getting packages from git or github into Service Designer.

If you want to have it quick and easy,

  • Copy the directory Demo_File_Converter (located inside ISPackages ) inside C:\Programs\wMServiceDesigner\IntegrationServer\packages (This is just for demo purposes and not a recommended way to deploy packages to a Microservice Runtime or Inetgration Server).
  • Go to it's admin page (http://localhost:5555/)
  • On the left side expande the Packages section, click Packages-Management
  • Click Activate Inactive Packages
  • Select Demo_File_Converter.
  • Click Activate Packages

Build the solution

Create the package and folder structure

In Package Navigator rightclick on  the server and select New - Package.

Name the Package Demo_File_Converter.

RIghtclick the new package and select New - Folder.

Name the Folder DemoFileConverter.

Create 4 subfolders namedDocuments, Flatfile, FTP and Services.

Create the flatfileschema

Flat file parsing in webMethods is commonly done using Flat File Schemas which describe the structure of a flat file. The schema especially holds details like typ (delimited, fixed length), record and field identifiers and the record structure. A flat file schema may describe fiels consisting of different record types, if we have the means to distinguish the record types. The structure of the records (the fields) can be described in the schema as well but usually is done in a flat file dictionary which is then referenced in the schema.

Rightclick on the folder Flatfile and select New - Flat File Schema.

Name the Schema bankStatement.

Select Create File from Sample and use the Browse button to select the sampel file. The Sample file used here can be found in the Ressources folder within the package e.g. C:\Programs\wMServiceDesigner\IntegrationServer\packages\Demo_File_Converter\resources. Click Nect.

Make sure to set Sample has a record identifier No. Yes will only be used if we have multiple different record types. Click Next.

Select Delimited as Record Parser Typ. Click Next.

In the next dialogie we will select the separators for lines and fields. Record should already be prepolulated with carriage return / linefeed. For Field choose ";". Observe the preview showing the recognized columns. Click Next.

Designer geneartes the schema and a dictionary for the records. Click finish.

Review the Flat File Definition Tab. Record Identifier should be set tto Nth field and Default Record shouldpoint to the generated Dictionary. Default Record will be used for all records which are not identified by the Record Identifier.

Go to the FlatFile Structure tab. We want to exclude the header line from the parsing process. As the header line consists of the column names, we use the first column name as "record identifier". In the previous tab we made sure that the record identifier is the first (0th) field of every record. The record parser matches the content of the identifying field with the name we give the record in the record structure of the schema. Therefore rightclick the recorname bankStatement, select rename and rename it to Auftragskonto, as this is the first column name.

All record with different content in the first column will be parsed as default record.

Click the Create Document Type button in the top pane.  Save your work.

The created document type will appear beside the schema and dictionary. It will mirror our document structure with teh header line and several "recordWithNoID" before, after and one level below the header id together with some internal fields.

Create the document types

We already have a document type for holding the data read from the file, but we want to have a dedicated business representation of the data. Therefore we create 2 new document types. One for a single line and a secong one holding a complete list. In this example we will choose the easy path and have the field identical to the file, in real life those structure quite often differ.

Rightclick on the Documents folder and select New - Document Type.

Name the document type bankStatement.

Go to the generated document type in the flatfile folder bankStatementDT (open it by doubleclick if it is not open anymore), select the fieldnames below Auftragskonto including the internal fields @record-id and @segment-id and copy them.

Go to the new document type bankStatement and select insert to paste the fields.

Save your work. Create a second new documenttype and name it bankStatementList. From the palette drag and drop a Document Reference List to the canvas. In the dialog which opens select the bankStatement as document.

Cfter clicking ok the name of the new reference will be directly editable. If not rightclick the reference and select rename. Name it bankStatementList.

Save your work.

Create the main services

parseBankStatementFile

This will do the actual parsing and conversion into an internal document type. Rightclick the folder Services and select New - Flow Servic.

Make sure the Input/Ouptut tab is active. Drag a String variable from the palette on the right side to the Inputr pane. If the palette is not visible, click the small triangle in the upper right corner of the output pane. Drag and drop a Document Reference to the output pane. In the dialog opening select the bankStatementList Document we created.

Switch to the tree tab. In the Package Navigator expand WmFlatFile - pub - flatFile. Drag and drop the service convertToValues to the canvas. In the Pipeline tab in the Pipeline In pane there is the variable inData we defined as input for the service. Click and drag this to the input ffData of the service convertToValues. Doubleclick the input variable ffSchema of the service or select it an click the setValue Icon in the upper bar of the Pipeline tab. Insert the qualified name of our flatfileschema: DemoFileConverter.Flatfile:bankStatement.

Drag and drop a Document Reference from the Palette to the Pipeline Out pane. In the dialogue opening select the bankStatementDT document type from the FlatFile folder. Map ffValues from ServiceOut to this document reference. Drop the variables ffData, ffSchema, ffValues and ffIterator from the pipeline by selecting them and clicking the downward Drop the selected Variable button in the upper pane of the pipeline tab.

Drag and drop a map step from the palette into the Tree. In the pipeline pane drag and drop a Document Reference to the Pipeline Out pane. Expand this document and the bankStatementFileLines document in the Pipeline Input pane. Drag the subnode Auftragskonto of bankStatementFileLines to to the subnode bankStatementList of the document bankStatementList. Drop the document bankStatementFileLines from the pipeline.

Add another map step. Expand the bankStatementList. Drop the internal reference fields @record-id and @segment-id as we do not want to have them in the generated xml or json. Save your work.

bankStatementFileToJson

Create a new service called bankStatementFileToJson in the folder Services.

Create a Sting variable named inData as input and outJson as output.

Switch to the Tree tab. Drag the service DemoFileConverter.Services:parseBankStatementFile to the canvas.

Expand the package wmPublic to pub.json and drag documentToJSONString to the canvas below the last service. Map the serbice output JsonString to outJson. Drop the variables document, bankStatementList and jsonString from the pipeline. Save your work.

To test the service rightclick on DemoFileConverter.Services:bankStatementFileToJson and select Run - Run As Flow Service.

Click the 3 dotes beside the edit field of inData top open the larger editor. Paste the sample content into the editor:

Auftragskonto;Buchungstag;Valutadatum;Buchungstext;Verwendungszweck;Beguenstigter/Zahlungspflichtiger;Kontonummer;BLZ;Betrag;Waehrung;Info
DE97100100000000123456;01.10.2019;03.10.2019;debit note;Birdseed;ACME;DE85200100000000654321;20010000;300,00;EUR;
DE97100100000000123456;02.10.2019;04.10.2019;debit note;Springs;EMCA;DE17300300000000333555;30030000;500,95;EUR;

Click OK. Go to the results tab andclick the field outJson to see its content.

bankStatementFileToXML

This service is build the same way like bankStatementFileToJson, just name the ouput field outXML and instead of pub.json:documentToJSONString use pub.xml:documentToXMLString from WmPublic.

Provide the services via FTP

Define the ftp port

For calling the service fir ftp we need a ftp port. If we access the Integration Server directly via ftp, the file will not be written to disc butr directly passed to the service. Open the Integration Server Admin page (http://localhost:5555/). Go to Security - Ports.

Click Add Port.

Select webMethods/FTP.

Enter

Port: 8021

Alias: ftp

Package Name: Demo_File_Converter

Click Save Changes.

Per default the Access Mode is set do deny by default, that means, every service called via this port must be explicetly allowed. For this example we will change this to Allow by Default. This is not reccommended for real life scenrios!

Click the link Deny + in the Acces Mode column of the ftp port.

Click Set Access Mode to Allow by Default.

Confirm with OK.

Output templates for the ftp wrappers

webMethods Integration Server and Micxroservice runtime can use output templates to format output of services in various formats. For historical reasons the templates have either the ending html or xml. *.html is used for html formatting. *.xml can be used for all the other. A template with extension .xml can return formats other than xml and we will use this for our json output.

Go to the template directory within our package directory, e.g. C:\ServiceDesigner\IntegrationServer\packages\Demo_File_Converter\templates. Create 2 files there,

bankStatementFileToJson_ftpOutput.xml with content %value outJson encode(none)%

bankStatementFileToXml_ftpOutput.xml with content %value outXML encode(none)%

This will insert the content of the according output variables into the generated file. As outXML will already be a complete xml string we do not need to add additional tags here.

FTP wrapper for bankStatementFileToJson

For accepting input via FTP we create wrapper services which accept the ftpdata, convert the input stream to a string and apply an output template to return the output as a file.

Rightclick the ftp folder and select New - Flow Service.

Name the service bankStatementFileToJson.

Drag an object from the palette to the input pane and name it ffdata. A file sent to the ftp port will be passed o the service as stream named ffdata. Drag a string varianle to the output pane and name outJson.

In the packagenavigator go to WmPublic - pub - io  and drag the service streamToString into the canvas. Map ffdata to inputStream. Drop inputStream from the output.

Advanced note: We made the input for our transformer services a string to be as flexible as possible for reusage in other entry methods. For very big files you cann also pass a stream to the convertToValues service and have a node iterator process the file in parts, so you do not need to load the complete file into memory at one. Please look into the Flat_File_Schema_Developers_Guide for more details.

Drag  the service DemoFileConverter.Services:bankStatementFileToJson to the canvas below streamToString. Map string, the ouput of the former service, to indata.outData is automaticalyl assigned to outJson of the actualservice bevause of the identical name, but you may still map the 2 together to indicate this mapping is intended.

Go back to the Input/Output tab. In the proprties pane write bankStatementFileToJson_ftpOutput.xml into the field Output Template - Name. Thisis the name of the template which will be used. Although the extension is xml, this can be as well used to generate arbitrry other formats like json.

FTP wrapper for bankStatementFileToXML

Build the service the same like FTP wrapper for bankStatementFileToJson , just insert bankStatementFileToXml_ftpOutput.xml as output template name and

insert DemoFileConverter.Services:bankStatementFileToXML instead of DemoFileConverter.Services:bankStatementFileToJson as the second service.

Provide the services via filepolling on local directories

Create the local directories

A filepoller can monitor a directory where Integration Server or Microservice Runtime has access to. This directory needs to be accessible locally via standard fileoperations, but this include accessible shares in windows or volumes in a docker environment. For this demo I created directories on the C drive as below:

C:\FilePoller\Json\in
C:\FilePoller\Json\work
C:\FilePoller\Json\done
C:\FilePoller\Json\error

If you do not have write access to C or do want to have his in a different location you may choose one, just set the paths in the below configuration accordingly.

Althoug you can use only one directory for a filepoller it is recommended to use 4, so the flow is as follows:

  1. File arrives in Monitoring Directory, here C:\FilePoller\Json\in
  2. File is renamed (with FilePolling and timestamp as prefix) and moved to Working Directory (optional), here C:\FilePoller\Json\work
  3. After successfull processing the file is moved to Completion Directory (optional), here C:\FilePoller\Json\done
  4. In case of an error the file is moved to the Error Directory (optional), here C:\FilePoller\Json\error

To keep the example easy we will write the output file to the Working Directory just with the extension of the target format (e.g. .json).

Create the processing service for the poller

Again we need a wrapper service, as we want to write the ourput data to a file. If not already present create a folder FlatfilePolling. Create a new flow service in this folder and name ot bankStatementFileToJson.

In the Input/Output tab. Drag a variable of type object to the input pane and name it ffdata. Drag 2 string variables into the input pane and call them fileName and originalFileName. Those will be populated at runtime from the filepoller. ffdata will be the input stream of the found file, fileName the working path and name of the file while in the working directory and originalFileName the original path and name before transfered to the working directory.

In the Tree tab drag the service streamToString from pub.io in the Package Navigator to the canvas. Map ffdata to inputStream in ServiceIn.

Drag our service DemoFileConverter.Services:bankStatementFileToJson to the canvas below streamToString. Be sure to choose the service from the folder DemoFileConverter.Services. Map string (the output of the previous step) to inData. Drop inData and string from the pipeline.

Drag pub.file:stringToFile from the package WmPublic into the canvas. Map outJson to data. Use the tSet Value button on fileName to set it to %fileName%.json. make sure to have Perforam pipeline variable substitution checked. This way we will get the output file in the working directory with .json attached to the end. Set append to false. Save your work.

Set up the file poller

Filepoller are setup as ports, although they do not open a tcp/ip port. On the Microservice Runtime Administrator page go to Security - Ports. Click Add Port.

Select webMethods/FilePolling- Click Submit.

Enter

Run services as user Administrator
Alias JsonBankStatementFilePoller
Monitoring Directory C:\FilePoller\Json\in
Working Directory (optional) C:\FilePoller\Json\work
Completion Directory (optional) C:\FilePoller\Json\done
Error Directory (optional) C:\FilePoller\Json\error
Enable Yes
Processing Service DemoFileConverter.FlatfilePolling:bankStatementFileToJson
File Polling Interval (seconds) 5
Maximum Number of Invocation Threads 10

If you choose different directories you need t oadjust thoose values. Click Save Changes.

Per default the Access Mode is set do deny by default, that means, every service called via this port must be explicetly allowed. For this example we will change this to Allow by Default. This is not reccommended for real life scenrios! Click the link Deny + in the Acces Mode column of the ftp port.

Click Set Access Mode to Allow by Default.

Confirm with OK.

Check if the port is enabled.

Set write permission to the output dir

The called service will write the result back to the working directory. For security reasons local writes of Integration Server / Microservice Runtime need to be authorised. This is done in the file file AccessControl.cnf in the directory config of the WmPublic package, e.g. in C:\Programs\ServiceDesigner\IntegrationServer\packages\WmPublic\config.

Add the work directoy to the allowedWritePaths of this file, if you left the paths as in my example, the file should look like

allowedWritePaths=C:\\FilePoller\\Json\\work
allowedReadPaths=
allowedDeletePaths=

Try it out

Use it via FTP

Make sure, the FTP port is enabled. The example below it is on port 8021.


An example file can be found in the Ressources dirctory of this package, if you may need to adjust the local path according to the location of your Installation:

Get Json via ftp

cd C:\wMServiceDesigner\IntegrationServer\packages\Demo_File_Converter\resources
ftp
open localhost 8021
cd /ns/DemoFileConverter/ftp/bankStatementFileToJson
put DemoBankStatement01.csv file_01.csv;application:x-wmflatfile
get file_01.csv

Get XML via FTP

cd C:\wMServiceDesigner\IntegrationServer\packages\Demo_File_Converter\resources
ftp
open localhost 8021
cd /ns/DemoFileConverter/ftp/bankStatementFileToXML
put DemoBankStatement01.csv file_02.csv;application:x-wmflatfile
get file_02.csv

Inspect the output files in your local directory.

Use filepoller

Get Json via File Poller

Copy the sample file DemoBankStatement01 from C:\Programs\ServiceDesigner\IntegrationServer\packages\Demo_File_Converter\resources to C:\FilePoller\Json\in (or use the according directories in your installation).

After the polling interval (5 seconds in the example) the poller will pick up the file and the processing service will write the ouput to the work directory (e.g. C:\FilePoller\Json\work), with the extension .json and the prefix FilePolling and a timestamp.

The processed input file will be moved to the completion directory (e.g. C:\FilePoller\Json\done).

7e5d9236-cb4e-42b2-bb23-b3c89cdfc2b6.png

18a32dcb-4009-48fd-8d05-baa27b3c9ba7.png

1c884591-766e-4b8a-bdcc-8feed973c746.png

0d67d493-b6b1-4596-abb8-ffb7996580fd.png

e6bdd8f0-4657-46aa-9124-5b09f1dfedba.png

5416f5ef-3ed0-40d3-bc52-0a4fc71890bb.png

15a289cb-a66e-4eda-a196-bde07ec1df1f.png

57eea25d-be23-40a1-9233-6c586c5f5f6c.png

dd542eaf-8386-447f-b0b4-e024343fde23.png

2c02b713-6218-44e2-85e2-9caf22992b6f.png

f2af50a3-09c9-498a-adce-bb95ec0cb2ab.png

a3ac2584-3db8-4f66-bc6e-5a11247ddcb8.png

a26815ad-bc58-48b9-8eb4-702309d2d796.png

f0966dc6-57d0-4a0b-ad07-8ca19bf07f7a.png

396282bc-9139-479a-ae37-eb65beb8e334.png

b0e75c2a-3979-4ed3-ba41-9cddfdc4e571.png

e74c1bb6-fac8-486a-874c-6e654ddde826.png

d867521a-638c-4eaf-bb62-13117c0d29cb.png

4357a1ff-c2f8-4aab-a675-e02bd0487e5f.png

033787fb-a335-4acb-8163-b4cc161d3148.png

4edc3133-6e49-46d4-861a-786301568650.png

bb334d63-c5db-4848-b9cc-ca45cfaa507c.png

fc27d3ef-8844-44ce-9074-2d6ffacfe168.png

d418e51a-d693-434c-8ab4-0b61bc349234.png

d6549cf8-6c2c-43c3-8a28-ea60831b57b9.png

010ff63d-e434-4cb2-9487-3c5e30e6e4f4.png

cac3ddce-6a42-45e6-85af-89a61afdfbe9.png

0236256c-5cb2-476f-ac43-234a4e458b71.png

0609bb36-9ae4-4dae-8d4e-70144776fabb.png

7676b4ad-9543-4d71-b12a-5b395c809617.png

ad618512-d918-4397-9240-203e04cbdad7.png

66628563-7109-438f-b29f-29cf4055342a.png

b5a2c3e1-863d-4444-99af-7d1ca5520a09.png

0bbff3b0-e9dd-47bd-a205-9ff88e39f0ff.png

69b3230c-cbd4-49a6-a236-83052a532ec1.png

ef735906-c933-4d06-9260-647cbc26c235.png

9f9ca5f3-d95f-4652-8feb-b886610fbdec.png

e02028ed-d3e2-4648-b695-279e60e83dfe.png

6939e26a-49e7-4b27-b681-604316da22df.png

e6176d34-1e69-4648-87c5-62143549caed.png

04495a95-88d4-42c3-81a7-f37663103999.png

4cae2b81-b810-4445-9a0e-8f66e8a343ed.png

d32fc6d7-632f-4531-8011-4b275628e46f.png

928dce40-5f0d-4444-b2dd-1142c93e986d.png

6eaa09d1-7c6e-4827-acfe-1108d9a5c5cd.png

3baaf2fa-5ce5-42d1-9a8c-933148baefc1.png

736a75f6-a6e3-41f0-bbf5-02478ee8a6f9.png

c995054a-7762-47dd-a52f-dc878e331498.png

7bd8d096-df93-4b38-9f1e-da38c0a42d36.png

305dd284-d1cf-4052-b559-680348d9aade.png

FTPJsonService001.png

c247e51b-d8a9-4c07-9f61-1a612c072b61.png

d26049b0-99ac-4d88-ba57-e9d7b51fff2c.png

35cc8d7f-6e01-417e-bd47-6863e1f5eac0.png

a7723251-582d-4fff-8842-7a2a6875f885.png

90922098-af27-4e9d-8129-3fc5cffc7e91.png

db2bd706-2575-4b09-ba3f-8754ebb461d6.png

ce775ccf-8e4d-4fd6-82ed-692119ac3cf4.png

58fb60f6-12c4-4767-ac7c-7b1d77105881.png

f54cefc7-6bc2-498a-99ae-8754d7e5ee50.png

aa490411-729a-4d14-b6a0-4a78f2ee48a9.png

36f791f4-f25b-4745-aa5f-f74c3eaf7115.png

066614b0-e5a9-4b70-b11e-f04cd3e348d7.png

3b3b3754-7188-4a81-8851-5d33ef6c9b8f.png

a6569e82-64a0-49b1-a485-16bdd5dc7cbf.png

f700e880-8985-4c0f-b578-e5632f705f39.png

7f7061e6-66b2-42f8-9de6-c5106f427299.png

82e46546-5357-450d-9f57-d6e17c5187dd.png

c2bc12ac-6a9d-4874-9228-0a57853443e4.png

b33c6818-d16c-41a4-93e9-03a26a88e9a8.png

28f27265-5ab4-482a-a5e2-c8ffeb2d493f.png

6e91bb35-8a33-453d-a0ce-4e5ce4071321.png

90e82d57-fec6-42b8-9470-c899870aa326.png

f79b9000-4743-4251-b934-9b478cf75b57.png

01b6d443-96c1-4a2e-8cb3-f5c74b507c4e.png

a234f10c-64a0-48e7-a2fd-5a9a28bc3b26.png