Csv to Json
This example shows how to read data from a csv and transform it into a json - one time using strongly typed object and also using the dynamic ExpandoObject.
Transforming csv to json
This example demonstrates how to transform a csv into a json with only a few lines of code.
CsvSource
Let’s start with a simple example how to create a flat file source. In this scenario we are using the CsvSource.
As for the CsvSource, the ResourceType
is ResourceType.File
by default. It will read data from the path //share/demo.cvs
.
By default, the CsvSource will try to use the header columns of the file to propagate the data into the right properties of the CsvType object.
If you need to read a csv file from a webservice, your code would look like this:
Note
Reading the csv file data
Let’s assume your csv would look like this:
Then this should be your CsvType class:
Now you can use the CsvSource as source for either a transformation or any other destination. If you want to directly convert your data into Json, this would be your working code:
Using dynamic objects
For such simple flow you don’t necessarily have to create an object. You can use the default implementation of CsvSource and JsonSource, which would use an ExpandoObject. As we don’t need a strongly typed object here in this example, we could modify our code like this: