# Parse Data

> Parses a raw string into a structured data object.

Source: https://docs.sommatic.ai/en/nodes/data.parse/

- Slug: `data.parse`
- Family: Data
- Version: 1.0.0
- Status: Partial. Only `json` and `csv` are implemented; `xml` and `yaml` leave through `error` ("not supported yet"). CSV parsing does not handle quoted fields.
- Execution: sync

Parses a raw string into a structured data object.

Essential for handling incoming data from external systems, files, or legacy APIs that return data as serialized strings (JSON, CSV, XML, YAML).

**Supported Formats:**
- **JSON:** Standard JSON parsing.
- **CSV:** Comma-Separated Values (can output Array of Objects).
- **XML:** Converts XML to JSON structure.
- **YAML:** Parse YAML to JSON.

**Use Cases:**
- Converting an HTTP response body string into useable JSON variables.
- Parsing a CSV file uploaded by a user.

## Configuration

- `input_data` (string, optional): The string to parse. Use expressions to pick a specific field (e.g., {{ $json.raw_text }}). If empty, the entire payload is evaluated.
- `format` (string, optional): Default: `json`. Allowed values: `json`, `csv`, `xml`, `yaml`.
- `csv_options` (object, optional)
  - `has_header` (boolean, optional): Default: `true`.
  - `delimiter` (string, optional): Default: `,`.

Its configuration shows the shared execution settings, but this node does not apply retries (`retryOnFail`, `maxTries`) yet. `continueOnFail` is not applied by any node.

## Output ports

- `out` (default): Parsing successful.
- `error`: Parsing failed (e.g. malformed JSON).

## Input contract

- `raw` (string, required): The raw string to parse.

## Output contract

- `parsed` (any, optional): The structured result (Object/Array).

## In expressions

What a later node can read from this node's output with `$("Parse Data").item.json.<path>` (write your node's label instead of `Parse Data`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("Parse Data").item.json.parsed` (any): The structured result (Object/Array). **The worker does not return it: reads as undefined.**


## How it works

Parses a JSON or CSV string into structured data, which replaces the payload.

1. Resolves the config. The input is `input_data` when it is set and not empty; otherwise the whole incoming payload.
2. If the input is not a string: with `format` `json` an object is returned unchanged through `out`; anything else fails.
3. `json`: `JSON.parse`; the result (object, array or primitive) becomes the payload.
4. `csv`: splits lines on `\n` or `\r\n` and drops blank lines. With `csv_options.has_header` (default true) returns an array of objects keyed by the trimmed header names; without it, an array of arrays. Values stay strings.
5. `xml` and `yaml` are declared but not implemented: they fail.

## In a workflow

### Node

```json
{
  "node_id": "node-parse-csv",
  "label": "Parse inventory CSV",
  "operator_slug": "data.parse",
  "operator_version": "1.0.0",
  "config": {
    "input_data": "{{ $json.raw_csv }}",
    "format": "csv",
    "csv_options": {
      "has_header": true,
      "delimiter": ","
    }
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": [
    {
      "sku": "A-1",
      "qty": "3"
    },
    {
      "sku": "B-2",
      "qty": "5"
    }
  ],
  "port": "out"
}
```

## Output ports (behaviour in the code)

### `out`

Parsing succeeded. The parsed value is the whole `data` (here, CSV with header).

```json
[
  {
    "sku": "A-1",
    "qty": "3"
  },
  {
    "sku": "B-2",
    "qty": "5"
  }
]
```

### `error`

The input is not a string, the text is malformed or the format is not implemented.

```json
{
  "ok": false,
  "errors": [
    "Parse Error (json): Unexpected end of JSON input"
  ],
  "port": "error"
}
```

## Behaviour in the code

- `csv_options`: `delimiter` (default `,`) and `has_header` (default true). Quotes are not handled: a delimiter inside quotes splits the field and the quotes are kept. Missing trailing values are left out of the row object.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `Input data is not a string` | `error` | `input_data` resolved to a non-string (or the payload is an object) and `format` is not `json`. Point `input_data` at the text field, for example `{{ $json.raw_csv }}`. |
| `Parse Error (json): <JSON.parse message>` | `error` | The text is not valid JSON. |
| `Parse Error (xml): Format xml not supported yet.` | `error` | `xml` (and likewise `yaml`) has no parser. |

## Notes and limits

- The output contract mentions `parsed`, but the parsed value replaces the payload directly.
- If the parsed value is falsy (`0`, `false`, `null`, `""`), the engine stores the whole envelope `{ ok, data, port }` as the output instead.
- When the envelope has no `data` (or `data` is falsy), the engine stores the whole envelope as the node output: on `error` the next node receives `{ ok: false, errors: [...], port: "error" }` as its payload.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "data.parse",
  "name": "Parse Data",
  "category": "data",
  "version": "1.0.0",
  "description": "Parses a raw string into a structured data object.\n\n  Essential for handling incoming data from external systems, files, or legacy APIs that return data as serialized strings (JSON, CSV, XML, YAML).\n\n  **Supported Formats:**\n  - **JSON:** Standard JSON parsing.\n  - **CSV:** Comma-Separated Values (can output Array of Objects).\n  - **XML:** Converts XML to JSON structure.\n  - **YAML:** Parse YAML to JSON.\n\n  **Use Cases:**\n  - Converting an HTTP response body string into useable JSON variables.\n  - Parsing a CSV file uploaded by a user.",
  "capabilities": [
    "parsing",
    "format-conversion",
    "transform"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "input_data": {
        "type": "string",
        "title": "Input Data",
        "description": "The string to parse. Use expressions to pick a specific field (e.g., {{ $json.raw_text }}). If empty, the entire payload is evaluated."
      },
      "format": {
        "type": "string",
        "enum": [
          "json",
          "csv",
          "xml",
          "yaml"
        ],
        "default": "json",
        "title": "Format"
      },
      "csv_options": {
        "type": "object",
        "properties": {
          "has_header": {
            "type": "boolean",
            "default": true
          },
          "delimiter": {
            "type": "string",
            "default": ","
          }
        }
      },
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    }
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "raw": {
        "type": "string",
        "description": "The raw string to parse."
      }
    },
    "required": [
      "raw"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "parsed": {
        "type": "any",
        "description": "The structured result (Object/Array)."
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Success",
      "is_default": true,
      "description": "Parsing successful."
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Parsing failed (e.g. malformed JSON)."
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "json",
    "csv",
    "xml",
    "parser"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_link_loom",
    "profile": {
      "name": "Link Loom",
      "verified": true,
      "url": "https://me.veripass.com/link-loom",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "input_data": {
      "type": "string",
      "title": "Input Data",
      "description": "The string to parse. Use expressions to pick a specific field (e.g., {{ $json.raw_text }}). If empty, the entire payload is evaluated."
    },
    "format": {
      "type": "string",
      "enum": [
        "json",
        "csv",
        "xml",
        "yaml"
      ],
      "default": "json",
      "title": "Format"
    },
    "csv_options": {
      "type": "object",
      "properties": {
        "has_header": {
          "type": "boolean",
          "default": true
        },
        "delimiter": {
          "type": "string",
          "default": ","
        }
      }
    },
    "execution": {
      "type": "object",
      "title": "Execution Settings",
      "properties": {
        "alwaysOutputData": {
          "type": "boolean",
          "title": "Always Output Data",
          "description": "Always output data, even on error (if configured).",
          "default": false
        },
        "retryOnFail": {
          "type": "boolean",
          "title": "Retry On Fail",
          "description": "Retry the execution if it fails.",
          "default": false
        },
        "maxTries": {
          "type": "integer",
          "title": "Max Retries",
          "description": "Number of retries.",
          "default": 3,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "waitBetweenTries": {
          "type": "integer",
          "title": "Wait Between Retries (ms)",
          "default": 1000,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "continueOnFail": {
          "type": "boolean",
          "title": "Continue On Fail",
          "description": "Do not crash workflow on error.",
          "default": false
        },
        "timeout_ms": {
          "type": "integer",
          "title": "Timeout (ms)",
          "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
          "default": 300000
        }
      }
    }
  }
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "raw": {
      "type": "string",
      "description": "The raw string to parse."
    }
  },
  "required": [
    "raw"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "parsed": {
      "type": "any",
      "description": "The structured result (Object/Array)."
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Success",
    "is_default": true,
    "description": "Parsing successful."
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Parsing failed (e.g. malformed JSON)."
  }
]
```
