# Input

Input task is triggered manually by the user. Before it runs, it will ask you to upload a file. The uploaded file is then available in the code editor via the preloaded `file` object.

### The `file` object

The code editor has a `file` object preloaded. Unlike other preloaded objects, `file` is **not** a parsed array of rows. Instead, it is an object with the following properties:

| Property       | Type   | Description                                                                     |
| -------------- | ------ | ------------------------------------------------------------------------------- |
| `file.name`    | string | The original filename including extension (e.g. `"products.csv"`, `"feed.xml"`) |
| `file.content` | string | The raw text content of the uploaded file                                       |
| `file.size`    | number | The file size in bytes                                                          |

Because `file.content` is a raw string, you need to parse it yourself using the appropriate filter for your file format.

### Parsing file content

Use the filename to detect the format and parse accordingly:

```liquid
{% assign content_parsed = "" %}

{% if file.name contains ".csv" %}
  {% assign content_parsed = file.content | parse_csv %}
{% elsif file.name contains ".xml" %}
  {% assign content_parsed = file.content | parse_xml %}
{% else %}
  {% log file.content %}
{% endif %}

{% if content_parsed %}
  {% for row in content_parsed %}
    {% log row %}
  {% endfor %}
{% endif %}
```

#### CSV files

The `parse_csv` filter parses a CSV string into an array of objects. The first row is used as headers — each subsequent row becomes an object with header names as keys.

```liquid
{% assign rows = file.content | parse_csv %}
{% for row in rows %}
  {{ row["Your Column Name"] | log }}
{% endfor %}
```

#### XML files

The `parse_xml` filter parses an XML string into a Liquid object. Elements become object keys, text content becomes values, and repeated elements become arrays.

```liquid
{% assign data = file.content | parse_xml %}
{% for product in data.products.product %}
  {% log product.title %}
{% endfor %}
```

#### JSON files

For JSON files, use the `parse_json` filter:

```liquid
{% assign data = file.content | parse_json %}
{% log data %}
```

#### Plain text and other formats

If the file is not CSV, XML, or JSON, you can work with `file.content` directly as a string. Use Liquid string filters like `split`, `strip`, etc.

```liquid
{% assign lines = file.content | split: "\n" %}
{% for line in lines %}
  {% log line %}
{% endfor %}
```

### Example: GraphQL mutation with CSV import

This example reads a CSV file with an `email` column and creates a Shopify customer for each row:

```liquid
{% capture mutation %}
  mutation customerCreate($input: CustomerInput!) {
    customerCreate(input: $input) {
      customer {
        id
      }
      userErrors {
        field
        message
      }
    }
  }
{% endcapture %}

{% json variables %}
  {
    "input": {
      "email": ""
    }
  }
{% endjson %}

{% assign rows = file.content | parse_csv %}

{% for row in rows %}
  {% assign variables.input.email = row['email'] %}
  {% assign result = mutation | graphql: variables %}

  {% if result.customerCreate.userErrors != empty %}
    {{ result.customerCreate.userErrors[0] | log }}
  {% else %}
    {{ "Created new customer with id: " | append: result.customerCreate.customer.id | log }}
  {% endif %}
{% endfor %}
```

Lines 1-13 capture a GraphQL mutation for creating a customer. Lines 15-21 define the variables required for the mutation. The CSV file is then parsed using `parse_csv`, and the `for` loop iterates through each row, assigning the row's email to `variables.input.email`. With mutation input prepared, the GraphQL query executes and creates the customer.

### Example: XML product feed import

```liquid
{% assign catalog = file.content | parse_xml %}
{% assign products = catalog.catalog.products.product %}

{% for product in products %}
  {% log product.sku | append: ": " | append: product.name | append: " ($" | append: product.price | append: ")" %}
{% endfor %}
```
