# Stringify Data

> Serializes a structured data object into a string format.

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

- Slug: `data.stringify`
- Family: Data
- Version: 1.0.0
- Status: Available
- Execution: sync

Serializes a structured data object into a string format.

This is the inverse of 'Parse Data'. It is useful when you need to send a JSON object as a raw string body to an API, save a configuration to a text file, or generate a CSV report from a list of items.

**Supported Formats:**
- **JSON:** Standard JSON serialization (with optional pretty-print).
- **CSV:** Converts a list of objects to Comma-Separated Values.
- **XML:** Converts an object tree to XML.
- **YAML:** Converts an object to YAML.

**Use Cases:**
- preparing a payload for an `http.request` body.
- Generating a downloadbale CSV export.

## Configuration

- `format` (string, optional): Default: `json`. Allowed values: `json`, `csv`, `xml`, `yaml`.
- `pretty` (boolean, optional): If true, adds indentation/newlines (JSON/XML only). Default: `false`.

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): Serialized
- `error`: Error

## Input contract

- `data` (any, required): The data structure to serialize.

## Output contract

- `string` (string, optional): The serialized string.

## In expressions

What a later node can read from this node's output with `$("Stringify Data").item.json.<path>` (write your node's label instead of `Stringify 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.

- `$("Stringify Data").item.json.string` (string) = `"{\"order_id\":\"ord_123\",\"total\":42.5}"`: The serialized string.


## How it works

Serializes the whole incoming payload to JSON, CSV, XML or YAML and returns it as `{ string }`.

1. Resolves the config; `format` defaults to `json` and `pretty` to false. There is no key to pick a field: the input is always the whole payload.
2. `json`: `JSON.stringify`, indented with 2 spaces when `pretty` is true.
3. `csv`: the payload must be an array of objects, or an object whose values are all objects. The header row is the keys of the first row; every value is JSON-encoded (strings in double quotes, `null` as `""`, missing as empty); rows are joined with `\n`.
4. `xml`: fast-xml-parser `XMLBuilder`, wrapped in `<root>`; `pretty` indents the output.
5. `yaml`: js-yaml in block style, indented with 4 spaces when `pretty` is true, otherwise 2.
6. Returns `{ string }` through `out`. An unknown `format` returns `String(payload)`.

## In a workflow

### Node

```json
{
  "node_id": "node-to-json",
  "label": "Serialize order",
  "operator_slug": "data.stringify",
  "operator_version": "1.0.0",
  "config": {
    "format": "json",
    "pretty": false
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "string": "{\"order_id\":\"ord_123\",\"total\":42.5}"
  },
  "port": "out"
}
```

## Output ports (behaviour in the code)

### `out`

Serialization finished.

```json
{
  "string": "{\"order_id\":\"ord_123\",\"total\":42.5}"
}
```

### `error`

The serializer threw.

```json
{
  "ok": false,
  "errors": [
    "Stringify Error: <message>"
  ],
  "port": "error"
}
```

## Behaviour in the code

- `pretty`: Also affects `yaml` (indent 4), not only JSON and XML as the schema says.
- `format`: `csv` with a payload that is not tabular (for example `{}` or an object with scalar values) returns `"[object Object]"` through `out`, not an error.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `Stringify Error: <message>` | `error` | The XML or YAML library could not serialize the payload. |

## Notes and limits

- CSV columns come from the first row only: keys that appear only in later rows are dropped.
- The input contract mentions `data`, but the whole payload is serialized. Shape it first with `data.transform` if you need one field.
- 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.stringify",
  "name": "Stringify Data",
  "category": "data",
  "version": "1.0.0",
  "description": "Serializes a structured data object into a string format.\n\n  This is the inverse of 'Parse Data'. It is useful when you need to send a JSON object as a raw string body to an API, save a configuration to a text file, or generate a CSV report from a list of items.\n\n  **Supported Formats:**\n  - **JSON:** Standard JSON serialization (with optional pretty-print).\n  - **CSV:** Converts a list of objects to Comma-Separated Values.\n  - **XML:** Converts an object tree to XML.\n  - **YAML:** Converts an object to YAML.\n\n  **Use Cases:**\n  - preparing a payload for an `http.request` body.\n  - Generating a downloadbale CSV export.",
  "capabilities": [
    "serialization",
    "format-conversion",
    "transform"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "format": {
        "type": "string",
        "enum": [
          "json",
          "csv",
          "xml",
          "yaml"
        ],
        "default": "json",
        "title": "Target Format"
      },
      "pretty": {
        "type": "boolean",
        "default": false,
        "title": "Pretty Print",
        "description": "If true, adds indentation/newlines (JSON/XML only)."
      },
      "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": {
      "data": {
        "type": "any",
        "description": "The data structure to serialize."
      }
    },
    "required": [
      "data"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "string": {
        "type": "string",
        "description": "The serialized string."
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Serialized",
      "is_default": true
    },
    {
      "port_id": "error",
      "label": "Error"
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "serialization",
    "json",
    "csv",
    "xml"
  ],
  "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": {
    "format": {
      "type": "string",
      "enum": [
        "json",
        "csv",
        "xml",
        "yaml"
      ],
      "default": "json",
      "title": "Target Format"
    },
    "pretty": {
      "type": "boolean",
      "default": false,
      "title": "Pretty Print",
      "description": "If true, adds indentation/newlines (JSON/XML only)."
    },
    "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": {
    "data": {
      "type": "any",
      "description": "The data structure to serialize."
    }
  },
  "required": [
    "data"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "string": {
      "type": "string",
      "description": "The serialized string."
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Serialized",
    "is_default": true
  },
  {
    "port_id": "error",
    "label": "Error"
  }
]
```
