Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Publish Functions

The publish functions serialize structured data into string formats (JSON or XML). They are typically used at the end of a workflow to prepare output data for transmission or storage.

publish_json

Serializes data from the source field to a JSON string.

Configuration

{
    "function": {
        "name": "publish_json",
        "input": {
            "source": "output",
            "target": "json_string"
        }
    }
}

Parameters

ParameterTypeRequiredDefaultDescription
sourcestring | JSONLogicYes-Field name in data to serialize (e.g., output or nested.field)
targetstring | JSONLogicYes-Field name where the JSON string will be stored
prettybooleanNofalseWhether to pretty-print the JSON output. Static, so the output shape is known at build time

source and target are JSONLogic, so either can be computed per message:

{
    "source": {"cat": ["outputs.", {"var": "data.format"}]},
    "target": {"cat": ["rendered_", {"var": "data.format"}]}
}

Both resolve to the name of a location, not to the value at one. The static spelling is a plain string, which is JSONLogic for itself: it folds to a constant at build time and keeps the precomputed path split, so nothing changes for the ordinary case.

Both name where the engine itself writes, and the destination is recorded in Change.path and on the audit trail, so neither may read {"secret": …} — see Secrets.

Examples

Serialize Data to JSON

{
    "id": "publish_response",
    "name": "Publish response",
    "function": {
        "name": "publish_json",
        "input": {
            "source": "response",
            "target": "responseBody"
        }
    }
}

Input:

{
    "data": {
        "response": {"status": "success", "count": 42}
    }
}

Result:

{
    "data": {
        "response": {"status": "success", "count": 42},
        "responseBody": "{\"status\":\"success\",\"count\":42}"
    }
}

Pretty-Print JSON

{
    "id": "publish_pretty",
    "name": "Publish pretty",
    "function": {
        "name": "publish_json",
        "input": {
            "source": "user",
            "target": "userJson",
            "pretty": true
        }
    }
}

Result:

{
    "data": {
        "userJson": "{\n  \"name\": \"Alice\",\n  \"age\": 30\n}"
    }
}

publish_xml

Serializes data from the source field to an XML string.

Configuration

{
    "function": {
        "name": "publish_xml",
        "input": {
            "source": "output",
            "target": "xml_string",
            "root_element": "Response"
        }
    }
}

Parameters

ParameterTypeRequiredDefaultDescription
sourcestring | JSONLogicYes-Field name in data to serialize
targetstring | JSONLogicYes-Field name where the XML string will be stored
root_elementstring | JSONLogicNorootName of the root XML element

source and target accept a computed value exactly as for publish_json, and so does root_element — so one task can name the document after the message it is serializing:

{"source": "output", "target": "xml", "root_element": {"var": "data.doc_type"}}

root_element is written into the serialized document that lands in data.{target}, so it may not read a secret either.

JSON to XML Conversion

The serializer follows these rules:

  • Object keys become XML element names
  • Array items are wrapped in <item> elements
  • Special characters are properly escaped (<, >, &, ", ')
  • Invalid XML element names are sanitized (e.g., names starting with numbers get an underscore prefix)

publish_xml is not the inverse of parse_xml. It has no notion of the $text and @name keys parse_xml produces — they are ordinary object keys, and sanitization turns them into <_text> and <_name> elements rather than a text node or an attribute. Lift the $text leaves with a map before publishing; see XML to JSON Conversion.

Examples

Serialize Data to XML

{
    "id": "publish_xml_response",
    "name": "Publish XML response",
    "function": {
        "name": "publish_xml",
        "input": {
            "source": "user",
            "target": "userXml",
            "root_element": "User"
        }
    }
}

Input:

{
    "data": {
        "user": {"name": "Alice", "age": 30}
    }
}

Result:

{
    "data": {
        "user": {"name": "Alice", "age": 30},
        "userXml": "<User><name>Alice</name><age>30</age></User>"
    }
}

Serialize Nested Data

{
    "id": "publish_nested",
    "name": "Publish nested",
    "function": {
        "name": "publish_xml",
        "input": {
            "source": "response.data",
            "target": "xmlOutput",
            "root_element": "Data"
        }
    }
}

Common Patterns

Complete API Pipeline

{
    "tasks": [
        {
            "id": "parse_request",
            "name": "Parse request",
            "function": {
                "name": "parse_json",
                "input": {"source": "payload", "target": "request"}
            }
        },
        {
            "id": "process",
            "name": "Process",
            "function": {
                "name": "map",
                "input": {
                    "mappings": [
                        {"path": "data.response.message", "logic": {"cat": ["Hello, ", {"var": "data.request.name"}]}}
                    ]
                }
            }
        },
        {
            "id": "publish_response",
            "name": "Publish response",
            "function": {
                "name": "publish_json",
                "input": {"source": "response", "target": "body"}
            }
        }
    ]
}

XML-to-XML Transformation

{
    "tasks": [
        {
            "id": "parse_xml",
            "name": "Parse XML",
            "function": {
                "name": "parse_xml",
                "input": {"source": "payload", "target": "input"}
            }
        },
        {
            "id": "transform",
            "name": "Transform",
            "function": {
                "name": "map",
                "input": {
                    "mappings": [
                        {"path": "data.output.result", "logic": {"var": "data.input.value.$text"}}
                    ]
                }
            }
        },
        {
            "id": "publish_xml",
            "name": "Publish XML",
            "function": {
                "name": "publish_xml",
                "input": {"source": "output", "target": "xmlResponse", "root_element": "Result"}
            }
        }
    ]
}

Generate Both JSON and XML Outputs

{
    "tasks": [
        {
            "id": "publish_json",
            "name": "Publish JSON",
            "function": {
                "name": "publish_json",
                "input": {"source": "response", "target": "jsonOutput"}
            }
        },
        {
            "id": "publish_xml",
            "name": "Publish XML",
            "function": {
                "name": "publish_xml",
                "input": {"source": "response", "target": "xmlOutput", "root_element": "Response"}
            }
        }
    ]
}

Error Handling

  • publish_json: Returns an error if the source field is not found or is null
  • publish_xml: Returns an error if the source field is not found or is null

XML Element Name Sanitization

XML has strict rules for element names. The publish_xml function automatically sanitizes invalid names:

OriginalSanitized
123field_123field
field namefield_name
field@attrfield_attr
`` (empty)_element

Next Steps