XML to String

Convert XML to String instantly in your browser.

Local Your input is processed locally in your browser. Found a problem? Report an issue

What XML to String does

XML to String reads XML and writes it as a JSON-escaped string. The input is parsed first, so a syntax error is reported with its position instead of producing broken output.

XML to String examples

The built-in sample

Input

<users><user id="1"><name>Ada</name></user><user id="2"><name>Lin</name></user></users>

Output

"{\"users\":{\"user\":[{\"@id\":\"1\",\"name\":\"Ada\"},{\"@id\":\"2\",\"name\":\"Lin\"}]}}"

This is the input that the Load sample button fills in.

The result is the JSON form of the XML, as one string

Input

<order id="7"><item sku="A1">Pen</item><item sku="B2">Ink &amp; paper</item><note><![CDATA[1 < 2]]></note><paid/></order>

Output

"{\"order\":{\"@id\":\"7\",\"item\":[{\"@sku\":\"A1\",\"#text\":\"Pen\"},{\"@sku\":\"B2\",\"#text\":\"Ink & paper\"}],\"note\":\"1 < 2\",\"paid\":\"\"}}"

The XML is first converted with the XML to JSON rules, then minified and escaped. The output is not the original XML text with escapes added.

Each output was produced by running this tool on the input above it.

How it works

Reading XML. The input is parsed with the browser's XML parser. Attributes become keys prefixed with @, text next to attributes or child elements goes into #text, repeated child elements become arrays, and every value is a string because XML has no data types.

Writing a JSON-escaped string. The data is minified and written as a single JSON string literal, with every quote and backslash escaped, ready to embed in code or another JSON document.

Details and limits

  • Use this when an API expects the JSON representation of an XML document inside a string field. If you need the XML text itself as a string literal, paste the XML into JSON Escape instead.
  • The mapping from XML is the one described on the XML to JSON page.
  • Input that is not valid XML stops the tool, and the status line shows where the problem is. The XML Validator page lists the usual causes.

Questions

Is my data uploaded when I use XML to String?

No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.

What does XML to String do?

XML to String reads XML and writes it as a JSON-escaped string. The input is parsed first, so a syntax error is reported with its position instead of producing broken output.

How does XML to String work?

Reading XML. The input is parsed with the browser's XML parser.

Are there any limits I should know about?

Use this when an API expects the JSON representation of an XML document inside a string field. If you need the XML text itself as a string literal, paste the XML into JSON Escape instead. The mapping from XML is the one described on the XML to JSON page.

Is there a size limit?

There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.

Does it work on phones, and is it free?

Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.