Skip to main content

xml_to_json

Parse an XML string into pretty-printed JSON, preserving the root element name as the top-level key.

Parameters​

ParameterTypeRequiredDescription
inputstringYesXML string to convert.

Response​

FieldTypeDescription
outputstringThe 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).

CodeWhenDelivered as
invalid_argumentsinput is missing or not a string.protocol error (invalid_params)
invalid_xmlThe input string is not valid XML or has no root element.tool error (isError)
internal_errorAn unexpected server error.protocol error (internal_error)

Mapping Convention​

XMLJSON
Element <foo>Object key "foo"
Attribute id="1""@id": "1" in the element's object
Repeated elements with same tagJSON 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_json and json_to_xml are not guaranteed to be lossless.