
We welcome contributions from anyone interested in improving this package! To get started, please follow these steps: Note that for fields where you want to retain the original value, specify null as the action. On the other hand, if the action is a function, it will be passed the field value as an argument, and whatever is returned from the respective function will be put in the final CSV. If the action is an object, such as ethnicityOptions, the value of the field will be used as the key to look up in the object. Lastly, if you want to look up a value in an object, define the object as the action for the field. For the date of birth field, you can calculate the age using the calculateAge method. For instance, for the admit date field, you can define a getDateString function that will receive the "admit_date" value as an argument and return the formatted string. The function will receive the value of the field as an argument and return the formatted string. To format a field, such as the admit date, define a function in the "actions" array for that field. For example, to merge the first name and last name fields, use profile.first_name_profile.last_name. To merge two fields, such as the first name and last name, use a double underscore (_) in the respective value in the "keys" array. This will automatically fetch the value of "name" and include it in the exported CSV. For instance, if you have an object that has an object inside it, and that object has another object inside it, you can simply get the value you want by using dot notation (e.g., ). Since JSON data can be deeply nested, you can traverse the path to the value you want to use by separating it with a dot. In the example above, we have an array of profiles that we want to transform into a CSV file. Now, let's use Jackson's CsvMapper to read our CSV file into a List of OrderLine objects.Import Explanation When we run this sample code, our example JSON document is converted to the expected CSV file. writeValue(new File("src/main/resources/orderLines.csv"), jsonTree) Then, we create a CsvMapper with our CsvSchema, and finally, we write the jsonTree to our CSV file: CsvMapper csvMapper = new CsvMapper() JsonNode firstObject = jsonTree.elements().next() įirstObject.fieldNames().forEachRemaining(fieldName -> ) ĬsvSchema csvSchema = csvSchemaBuilder.build().withHeader() To do this, we create a CsvSchema Builder and set the column headers to match the JSON field names: Builder csvSchemaBuilder = CsvSchema.builder() This determines the column headers, types, and sequence of columns in the CSV file. First, we use Jackson's ObjectMapper to read our example JSON document into a tree of JsonNode objects: JsonNode jsonTree = new ObjectMapper().readTree(new File("src/main/resources/orderLines.json"))
