Back to Blog
3 min. read

COBOL Copybook transformation to JSON

In this blog, we are going to learn what is a COBOL copybook and how to read and transform copybook type data into JSON format in mule4.

What is a COBOL Copybook?

A COBOL copybook is a type of flat file that describes the layout of records and fields present in the file.

Example of Copybook data:

Steps to convert the copybook into JSON

Step 1:

The first step is to generate a flat file schema definition i.e.  .ffd file from the above provided copybook (.cpy file) using Transform Message component.

Import a COBOL definition .cpy file into the Transform Message component by selecting the Set Meta-Data Type in the Transform message Input section.

Once the import of .cpy file is correctly done, the Transform component will then convert this .cpy file and generate the flat file schema definition and will store it inside the src/main/resources/schema folder of your Mule project.

The generated flat file schema file looks like below:

Step 2:

Drag and drop the Set Payload component from the Mule Palette to specify the MIME Type and the schema path of incoming payload. The MIME type should be “application/flatfile”. Under schemaPath specify the above generated .ffd file path.

Step 3:

Drag and drop the Transform Message component after the Set Payload for data conversion.

The final mule application now looks like this:

Code snippet:

										<flow name="cobol-to-jsonFlow" doc:id="85f0a0bd-94e4-4768-bd47-2e946c09e850" >
	<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="convert"/>
	<set-payload value="#[payload]" doc:name="Set Payload"	mimeType='application/flatfile;
		schemapath="schemas/employee.ffd"'/>
	<ee:transform doc:name="Transform Message">
	<ee:message >
		<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
Employees: payload.EMPLOYEES map () -> {
	EmpId: $.EMPNUMBER,
	Name: $.NAME,
	PhoneNumber: $.PHONENUMBER,
	Department: $.DEPT
}
}]]></ee:set-payload>
	</ee:message>
	</ee:transform>
	<logger level="INFO" doc:name="Logger" message="#[payload]"/>
</flow>
									

Test using Postman:

Request:

Response:

Conclusion:

This is how a COBOL copybook file is converted into a JSON file by using the Mule Transform Message component.

Happy Learning!

Contact us