There are many use cases where you will need to convert one data file format to another.
For example, you may need to transform a source data file to a format that will be used in the payload request to an API. Likewise, the response from an API could be in a format like JSON but the destination for your data requires a different format such as XML.
Alternatively, your requirements may be as simple as transforming some source data into an Excel spreadsheet so that it can be used for analysis or as part of another business process.
Reading various data file formats and converting them is a core requirement of data integration. In this demo, we will show you how to convert between any of the following formats: JSON, YAML, XML, CSV, and Excel.
watch this use case in action
Line by Line: How this "Data File Conversion" demo works
Reading and converting data file format to a different data file format.
While there are several services present in the demo for reading and saving various data file formats to a different data file format, the functions used, and the data reading and extraction logic are the same.
For reading formats like JSON, XML, or YAML, you will notice in the respective services that each service starts with File.resourceStream(line 3) and the corresponding function for getting the data by its InputStream.
For reading formats like CSV and Excel, similar to the other formats, it also starts with File.resourceStream. The difference for these data formats is that it uses the generated service from its corresponding Flatfile descriptor, with the input to the service being an Input since the service will be reading a file to get its contents.
The service flow for the following formats is as follows.
XML, JSON, YAML, CSV and Excel
Line 3: A placeholder model is declared, called contact (this step is only when converting a file format to an XML, JSON & YAML)
Line 4-5: The contents of the selected data file format are pulled.
Line 6-7: Each of the records in the pulled will be mapped to the contact model.
Line 8-9: An OutputStream File.packageOutputStream function is called to create an OutputStream where the data can be written. then is written to the OutputStream using the File.write function.