# CSV

{% hint style="info" %}
Version compatibility

`CSV` was introduced in version **4.28.0** of the JourneyApps Container.
{% endhint %}

### Generating a CSV String

#### Format

{% code title="main.js" %}

```javascript
CSV.stringify(source, options);
```

{% endcode %}

| Parameter            | Details                                                                                                                                                                                                                                                                                       |
| -------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `source`             | <p>One of:<br></p><ul><li>Array</li><li>Matrix (array of arrays)</li></ul>                                                                                                                                                                                                                    |
| `options` (Optional) | <p>Object with the following properties:<br></p><ul><li><code>heading</code>: Array of column headings. Use <code>null</code> to generate a CSV file without a header row.</li><li><code>delimiter</code>: Character to separate columns with.</li></ul><p><br>Defaults to <code>,</code></p> |

Returns: the generated CSV as a **string**.

#### Examples

{% code title="main.js" %}

```javascript
var mx = [['Cell Phone', 'Black'], ['Computer','Red']];
var csv_file = CSV.stringify(mx, {heading: ['Type', 'Color']});
```

{% endcode %}

In this case, `csv_file` is

{% code title="csv\_file.csv" %}

```
Type,Color
Cell Phone,Black
Computer,Red
```

{% endcode %}

Suppose we have an `asset` data model object with (amongst others) `name` and `cost` as fields, and we want to generate CSV for all assets and their names and costs:

{% code title="main.js" %}

```javascript
var allAssets = DB.asset.all().toArray();
var myMx = allAssets.map(function(asset) {
  return [asset.name, asset.cost];
});
var csv = CSV.stringify(myMx, {heading: ['Name', 'Cost']});
```

{% endcode %}

### Parsing a CSV String

#### Format

```javascript
CSV.parse(source, options);
```

| Parameter                                                                                                                         | Details                                          |
| --------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------ |
| `source`                                                                                                                          | String                                           |
| `options` (Optional)                                                                                                              | <p>Object with the following properties:<br></p> |
| `delimiter`: Character to separate columns with. If no `delimiter` is specified, the library detects and uses the appropriate one |                                                  |

Returns: the parsed CSV as an **array of arrays**.

#### Example

Assume `csv_file`'s content is:

{% code title="csv\_file.csv" %}

```
Type,Color
Cell Phone,Black
Computer,Red
```

{% endcode %}

{% code title="main.view\.xml" %}

```xml
<!-- In the View XML -->
<var name="csv_file" type="attachment" media="text/csv" />
<capture-file bind="csv_file" required="false" />
```

{% endcode %}

{% code title="main.js" %}

```javascript
var csvFileContent = view.csv_file.toText();
var parsed = CSV.parse(csvFileContent);
// Starting at 1 since the heading is in position 0
for (var i = 1; i < parsed.length; i++) {
    var item = parsed[i];
    var newAsset = DB.asset.create({
        type: item[0],
        color: item[1]
    });
    newAsset.save();
}
```

{% endcode %}
