Versions Compared

Key

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

...

Before you can work with such a file format, you have to convert (transform) this structure into a JSON structure. The same is true in case you would like to convert a given JSON document into a CSV structure, for example to upload to some external system

...

Transform CSV → JSON

For this, PIPEFORCE provides the transformer command transform.csv.json which expects a CSV file which complies with the RFC4189 standard in the body or as input parameter of the command and converts it to a JSON document which can then be used for further processing.

...

Code Block
languagejson
{
  "columnsCount": 1,
  "rowsCount": 3,
  "rows": [
    ["row1"],
    ["row2"],
    ["row3"]
  ]
}

Transform JSON → CSV

For transformation from a given JSON document to a CSV document, a conversion from JSON to a text structure is required. Since the conversion rules from a given JSON to CSV could vary a lot, a template based approach 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
# The JSON to be converted (can be any structure)
body: {
        "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"