Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

For transformation from a given JSON document to a CSV document, a conversion from JSON to a text structure is required. In such casesSince the conversion rules from a given JSON to CSV could be vary a lot, a template based approach can be used. Here is is often a good idea here.

Below you can find an example to convert a given JSON to CSV using the FreeMarker Transformer command:

Code Block
languageyaml
bodypipeline:
  - transform.ftl:
        # The input JSON
        model: {
                  "columnsCount": 3,
                  "rowsCount": 2,
                  "headers": ["firstName","lastName","age"],
                  "rows": [
                    {
                      "firstName": "Max",
                      "lastName": "Smith",
                      "age": "38"
                    },
                    {
                      "firstName": "Susann",
                      "lastName": "Mayr Wan",
                      "age": "44"
                    }
                  ]
              }
pipeline:
  - transform.ftl:    
        # The conversion rule from JSON -> CSV
        template: |
            "${body.headers[0]}", "${body.headers[1]}", "${body.headers[2]}"
            <#list body.rows as person>
                "${person.firstName}",  "${person.lastName}", "${person.age}"
            </#list>

This example will finally output a CSV like this:

Code Block
"firstName","lastName","age"
"Max","Smith","38"
"Susann","Mayr Wan","44"