json_to_xml
Convert a JSON string to XML, wrapped in a configurable root element.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
input | string | Yes | JSON string to convert. |
root_element | string | No | Name of the XML root element to wrap the output in (default: root). |
Response
| Field | Type | Description |
|---|---|---|
output | string | The converted XML string. |
Example
{
"input": "{\"name\": \"Alice\", \"age\": 30}",
"root_element": "person"
}
Result:
{
"output": "<person>\n <name>Alice</name>\n <age>30</age>\n</person>"
}
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, or root_element is not a string. | protocol error (invalid_params) |
invalid_json | The input string is not valid JSON. | tool error (isError) |
internal_error | An unexpected server error. | protocol error (internal_error) |
Mapping Convention
| JSON | XML |
|---|---|
Object key "foo" | <foo> child element |
Object key "@id" | Attribute id="..." on the parent element |
Object key "$text" | Text content of the parent element |
Array ["a", "b"] | Repeated sibling <foo>a</foo><foo>b</foo> elements |
null | Self-closing element <foo/> |
| String, number, bool | Element text content |
Notes
- Round-trips between
json_to_xmlandxml_to_jsonare not guaranteed to be lossless. In particular, numbers are serialized as text and parsed back as strings. - XML special characters (
<,>,&,",') in values are automatically escaped.