JSON to CSV Transformer has the function of receiving a JSON object and, from it, generating an array with the data for the CSV already formatted.

Take a look at the configuration parameter of the component:

  • Headers: configures the headers the component will use to process the text. The items are separated by comma and can have more than one input. It's a mandatory parameter and must be configured according to what you want to process.

  • Delimiter: delimiting symbol to be used in the text processing. By standard, this option is configured as a comma (","). It's a mandatory parameter that can use many symbols as a separator.

  • Print Headers: if activated, the option inserts in the result the previously configured headers as the first element of the resulting array.

  • Coalesce: if activated, and an input message value corresponds to some object/array, the input will be processed and the deployment will happen normally; otherwise, when receiving a value as object/array, an error will be presented as result and "false" will be assigned to the property “success”.

  • Fail On Error: if the option is enabled, the execution of the pipeline with error will be interrupted; otherwise, the pipeline execution proceeds, but the result will show a false value for the “success” property.

The component waits for a message with the property "data" in the JSON. The value of this property can be an array or an object. See next a simple example that shows the functionality of JSON to CSV Transformer:

Messages flow

Input

You must configure an input JSON in a pipeline with the JSON to CSV Transformer component. After adding it to the pipeline, it's necessary to configure the headers as product,price or the example won't work.

{
"data": [
{
"product": "Samsung 4k Q60T 55",
"price": 3278.99
},
{
"product": "Samsung galaxy S20 128GB",
"price": 3698.99
}
]
}

Output

With the configurations done according to the specifications above, the result will be:

{
"data": [
"Samsung 4k Q60T 55,3278.99",
"Samsung galaxy S20 128GB,3698.99"
]
}

JSON to CSV Transformer in Action

See below how the component responds to a situation and its specific configuration.

Informing a value as object with the configuration "Coalesce: false" and "Fail On Error: false"

With the pointed configurations, the JSON won't be processed and the result will be an error message with the property success: false

Input

{
"data": [
{
"product": {
"name": "Samsung 4k Q60T 55"
},
"price": 3278.99
},
{
"product": {
"name": "Samsung galaxy S20 128GB"
},
"price": 3698.99
}
]
}

Output

{
"success": false,
"message": "Property product is an object"
}

Informing a value as object with the configuration "Coalesce: true" and "Fail On Error: false"

With the pointed configurations, the JSON will be processed and the result will be a csv with the object correctly treated.

Input

{
"data": [
{
"product": {
"name": "Samsung 4k Q60T 55"
},
"price": 3278.99
},
{
"product": {
"name": "Samsung galaxy S20 128GB"
},
"price": 3698.99
}
]
}

Output

{
"data": [
"product,price",
"\"{\"\"name\"\":\"\"Samsung 4k Q60T 55\"\"}\",3278.99",
"\"{\"\"name\"\":\"\"Samsung galaxy S20 128GB\"\"}\",3698.99"
]
}

Informing a value as object with the configuration "Coalesce: false" and "Fail On Error: true"

With the pointed configurations, the JSON won't be processed and the deployment will be immediately interrupted.

Input

{
"data": [
{
"product": {
"name": "Samsung 4k Q60T 55"
},
"price": 3278.99
},
{
"product": {
"name": "Samsung galaxy S20 128GB"
},
"price": 3698.99
}
]
}

Output

{
"timestamp": 1603812645143,
"error": "Property product is an object",
"exception": "com.digibee.pipelineengine.exception.PipelineEngineRuntimeException",
"code": 500
}

Did this answer your question?