Gratis å bruke, uten konto

JSON til XML-konverterer

Gjør om JSON og XML i begge retninger på denne enheten. Gjentatte XML-elementer blir til JSON-arrays, og JSON-arrays blir til gjentatte elementer. Attributter bruker @name, og elementtekst bruker #text. Ingen fil lastes opp.

Objekter blir nestede elementer. Bruk @name for attributter og #text for elementtekst.

Konverteringen bevarer ikke datatyper: XML-tekst blir JSON-strenger. Nullverdier, tomme verdier, kommentarer og formatering kan bli endret ved konvertering fram og tilbake.

Hvordan gjør jeg JSON om til XML?

Velg konverteringsretning, lim inn gyldig JSON eller XML, angi eventuelt XML-rotnavnet og velg «Konverter». JSON-matriser blir gjentatte XML-elementer, mens gjentatte XML-barnenavn blir JSON-matriser.

Veiledning

Slik bruker du JSON til XML-konverterer

The JSON to XML Converter turns JSON text into an XML document and XML text back into JSON. Keys that start with @ become attributes, the key #text holds the text of an element, and arrays become repeated elements. It is meant for small samples, test data and quick checks. It does not validate against a schema, keep number or true-or-false types, or handle namespaces and DTDs.

Slik gjør du

  1. Pick the direction: JSON to XML, or XML to JSON. The matching input box and the root name field show or hide.
  2. Paste your text. For JSON to XML you can also set the name of the root element and an indent of 0, 2 or 4 spaces.
  3. Press Convert. The result appears in the output box, or a message explains what is wrong with the input.
  4. Use Copy output to copy the result, or Download output to save converted.xml or converted.json.

Eksempler

Attributes, arrays and special characters

Du skriver inn
Retning
JSON to XML
JSON
{"book":{"@id":"b1","title":"Dune & Co","tags":["sf","classic"],"price":9.5,"stock":null}}
Rotnavn
root
Innrykk
2
Du får
XML
<?xml version="1.0" encoding="UTF-8"?>\n<root>\n <book id="b1">\n <title>Dune &amp; Co</title>\n <tags>sf</tags>\n <tags>classic</tags>\n <price>9.5</price>\n <stock></stock>\n </book>\n</root>

The @id key becomes the attribute id. The array becomes two tags elements, the ampersand is escaped as &amp;, and null becomes an empty element. The chosen root name wraps everything.

Kjørt med verktøyets egen kode.

A top-level array

Du skriver inn
Retning
JSON to XML
JSON
[1,2,{"a":true}]
Rotnavn
list
Innrykk
4
Du får
XML
<?xml version="1.0" encoding="UTF-8"?>\n<list>\n <item>1</item>\n <item>2</item>\n <item>\n <a>true</a>\n </item>\n</list>

XML needs one root, so an array at the top is placed inside your root name as repeated item elements. Booleans and numbers are written as plain text.

Kjørt med verktøyets egen kode.

A key that is not a valid XML name

Du skriver inn
Retning
JSON to XML
JSON
{"first name":"Ada"}
Du får
Melding
Use a valid simple XML name: letters, numbers, underscore, dot or hyphen; it must not start with a number.

Element names cannot hold a space. Rename the key to first_name or first-name and convert again.

Kjørt med verktøyets egen kode.

XML back to JSON

Du skriver inn
Retning
XML to JSON
XML
<people><person id="42"><name>Ada</name><tag>math</tag><tag>computing</tag></person></people>
Innrykk
2
Du får
JSON
{"people":{"person":{"@id":"42","name":"Ada","tag":["math","computing"]}}}

The attribute gets an @ prefix, and the repeated tag elements are grouped into one array. The tool prints it indented by two spaces; it is shown on one line here. This result comes from reading the code, because the XML reader needs a browser and was not run here.

Ikke kjørt med verktøyet. Se på dette som en illustrasjon.

Når det er nyttig

  • Turn a sample JSON response into XML to test a system that only accepts XML.
  • Read an XML snippet as JSON to see its structure before writing code to parse it.
  • Prepare small fixtures for a unit test in both formats.
  • Learn how attributes, text and repeated elements are shown in each format.

Begrensninger

  • Input can have at most 200,000 characters, 64 levels of nesting, 20,000 elements, and 5,000 items in one array.
  • Element and attribute names must be simple: they start with a letter or underscore and then use letters, digits, underscore, dot or hyphen. In JSON to XML a name with a colon, such as a namespace prefix, is refused, and keys starting with # other than #text are refused.
  • Types are not kept. XML text always comes back as a JSON string, null and an empty string both turn into an empty element, and in XML to JSON, comments and processing instructions are dropped.
  • Nested arrays are flattened into repeated elements, so the inner grouping is lost. An attribute value must be a string, number or true/false, and an object or null there causes an error. XML with a DTD or entity declaration is blocked.

Tips

  • Rename keys with spaces or odd symbols in the JSON before converting, instead of fixing the XML afterwards.
  • Use #text together with @ attributes when an element has both a value and attributes.
  • Compare the round trip on a small sample first, since types and empty values will not come back the same.

Kilder

Vanlige spørsmål

How do I convert JSON to XML?

Choose JSON to XML, paste valid JSON, set a root name if you do not want root, and press Convert. Objects become nested elements, arrays become repeated elements, and keys that begin with @ become attributes of their parent element.

How are arrays written in XML?

Each array item becomes its own element with the same name as the key. Going back from XML, child elements that repeat under the same name are put into a JSON array. A single child comes back as a plain value, so a one-item array does not round-trip.

Why did my number turn into a string after a round trip?

XML has no number or true-or-false types, only text. In the XML to JSON direction every value is a string, even if it looks like 9.5 or true. Convert the values to the right type in your own code if you need that.

What is the difference between @ and #text?

A key starting with @ makes an attribute, such as @id for id="1". The key #text sets the text inside an element that also has attributes or child elements. Other keys that start with # are not allowed.

Why is my XML refused when it has a DOCTYPE?

Input that declares a DTD or an entity is blocked on purpose. Those features can be used to expand data or pull in outside content, so the tool does not process them. Remove the declaration and convert the plain XML.

Does this tool check my XML against a schema?

No. It only checks that the text is well-formed XML and within the size limits. It cannot say if the elements are the ones another system expects. Use a validator for your schema or standard if that matters.

Is my data uploaded when I convert?

No. The conversion happens in your browser and the code of the tool makes no network request. The XML reader of the browser does the parsing. Output is not saved, so copy or download it before you leave the page.

Hvordan gjør jeg JSON om til XML?

Velg konverteringsretning, lim inn gyldig JSON eller XML, angi eventuelt XML-rotnavnet og velg «Konverter». JSON-matriser blir gjentatte XML-elementer, mens gjentatte XML-barnenavn blir JSON-matriser.

Lastes JSON- eller XML-filen min opp?

Nei. Konverteringen kjøres lokalt i denne nettleseren. Inndata er begrenset til 200 000 tegn, og XML DTD-/entitetserklæringer blokkeres.

Hvordan representeres JSON-matriser i XML?

Hver matriseverdi blir et nytt element med samme navn. Ved konvertering tilbake grupperes gjentatte barnelementer med samme navn i en JSON-matrise. En matrise på øverste nivå pakkes inn i den valgte roten med barnelementer som heter item.

Bevarer JSON til XML datatypene?

Nei. XML-elementtekst har ingen JSON-type for tall eller boolske verdier, så XML til JSON returnerer tekstverdier som strenger. Nullverdier og tomme verdier kan også bli umulige å skille fra hverandre.

Støttes behandling av XML-DTD eller eksterne entiteter?

Nei. DTD-erklæringer og entitetserklæringer blokkeres. Verktøyet analyserer inndata lokalt og henter ikke eksterne ressurser.