XML/JSON 互转
Convert XML to JSON and back without leaving ComfyUI
- result_string
The LLM era made this node's job more common than it sounds. Your vision model or chat API returns XML - a structure tag or a tool call, maybe a whole formatted report - and everything downstream in your ComfyUI workflow wants JSON. Or the reverse: you have JSON and something you're calling wants XML. XMLJSONConverter is the bridge: paste text in, pick a direction, get the converted string out.
The controls:
text- the XML or JSON input (multiline).conversion_mode-xml_to_jsonorjson_to_xml. Both directions, one node.xml_root_name(optional, defaultelements) - used in thejson_to_xmldirection: when your JSON isn't already a single-key dict, the converter wraps it under this root element name so the XML is well-formed.
Under the hood it leans on the xmltodict library, which is the pragmatic way to do this and also the pack's reason for shipping that dependency. XML→JSON produces pretty-printed JSON with ensure_ascii=False, so CJK text stays readable. JSON→XML requires the JSON to be an object; anything else (a list, a scalar, or a dict with multiple top-level keys) gets wrapped in the root name first.
The sharp edges are the ones you'd expect from XML in general, and they're worth knowing before you trust the output:
- Attributes don't survive.
xmltodictfolds XML attributes into keys prefixed with@(<img width="5">becomes{"img": {"@width": "5"}}), and repeated elements can become lists or dicts depending on how many there are. The conversion is honest, but "converted XML" rarely looks the way a human would have written it. - Text mixed with child elements becomes a
#textkey. If you're converting XML with<p>hello <b>world</b></p>structure, the output has#textentries you'll need to handle downstream. - Errors don't throw - on a parse failure the node prints an error and returns the original input unchanged. That's forgiving, but it means "output equals input" is your signal that something failed, not an exception you'll see.
It's a small, honest utility: use it when a string-based tool in your graph insists on one format and you have the other. It will not prettify your life beyond the conversion - it will not preserve everything, and the round-trip XML → JSON → XML is lossy. For one-off data-shape fixes between nodes, it's exactly right.
Install
ComfyUI Manager → search comfyui-spawner-nodes → install → restart. Or:
cd ComfyUI/custom_nodes
git clone https://github.com/spawner1145/comfyui-spawner-nodes
Deps: xmltodict (the engine) plus piexif and pypng. All light pure-Python, no models. The README is a stub ("some toolkits"); author spawner1145 is a Chinese-speaking dev known for a Wan2.1 SD extension. UI labels are Chinese; the mode values are the English strings xml_to_json / json_to_xml.
Troubleshooting
- Output is identical to input - the conversion failed (probably malformed XML/JSON) and the node returned the input untouched. Paste the text into a validator to find the syntax error.
json_to_xmlwraps things inelements- you gave it a list or multi-key dict; it wrapped underxml_root_nameto stay valid. Set that field to whatever root you actually want.@attributeand#textkeys appear in XML→JSON output - normalxmltodictbehavior. Flatten them injson_processif a downstream node needs clean values.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| text | STRING | — | |
| conversion_mode | COMBO | 2 options: xml_to_json, json_to_xml | |
| xml_root_nameopt | STRING | elements | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| result_string | STRING | — |