xml_to_json
Parse an XML string into pretty-printed JSON, preserving the root element name as the top-level key.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
input | string | Yes | XML string to convert. |
Response
| Field | Type | Description |
|---|---|---|
output | string | The converted pretty-printed JSON string. |
Example
{
"input": "<person id=\"1\"><name>Alice</name><tag>admin</tag><tag>user</tag></person>"
}
Result:
{
"output": "{\n \"person\": {\n \"@id\": \"1\",\n \"name\": \"Alice\",\n \"tag\": [\"admin\", \"user\"]\n }\n}"
}
Errors
Each failure carries a precise code in the response. Argument-schema and server errors are JSON-RPC protocol errors; a rejected value is returned as a tool result with isError: true (so the agent can read the code and self-correct).
| Code | When | Delivered as |
|---|---|---|
invalid_arguments | input is missing or not a string. | protocol error (invalid_params) |
invalid_xml | The input string is not valid XML or has no root element. | tool error (isError) |
internal_error | An unexpected server error. | protocol error (internal_error) |
Mapping Convention
| XML | JSON |
|---|---|
Element <foo> | Object key "foo" |
Attribute id="1" | "@id": "1" in the element's object |
| Repeated elements with same tag | JSON array under that tag key |
Text-only element <foo>bar</foo> | String value "foo": "bar" |
| Mixed element (text + children) | "$text" key added to the element's object |
Self-closing element <foo/> | "foo": null |
Notes
- All attribute and text values are returned as strings. Type inference (e.g.
"42"→42) is not performed. - Round-trips between
xml_to_jsonandjson_to_xmlare not guaranteed to be lossless.