For a simple solution, I recommend Jackson, as it can transform arbitrarily complex JSON into XML with just a few simple lines of code.

import org.codehaus.jackson.map.ObjectMapper;

import com.fasterxml.jackson.xml.XmlMapper;

public class Foo
{
  public String name;
  public Bar bar;

  public static void main(String[] args) throws Exception
  {
    // JSON input: {"name":"FOO","bar":{"id":42}}
    String jsonInput = "{\"name\":\"FOO\",\"bar\":{\"id\":42}}";

    ObjectMapper jsonMapper = new ObjectMapper();
    Foo foo = jsonMapper.readValue(jsonInput, Foo.class);

    XmlMapper xmlMapper = new XmlMapper();
    System.out.println(xmlMapper.writeValueAsString(foo));
    // <Foo xmlns=""><name>FOO</name><bar><id>42</id></bar></Foo>
  }
}

class Bar
{
  public int id;
}

This demo uses Jackson 1.7.7 (the newer 1.7.8 should also work), Jackson XML Databind 0.5.3 (not yet compatible with Jackson 1.8), and Stax2 3.1.1.

Answer from Programmer Bruce on Stack Overflow
🌐
JSON Formatter
jsonformatter.org β€Ί json-to-xml
Best JSON to XML Converter
JSON to XML Online with https and easiest way to convert JSON to XML. Save online and Share.
Discussions

java - Simplest method to Convert Json to Xml - Stack Overflow
I have web-service in .net. When I retrieve data from database, it returns JSON File in Android Mobile. How can I convert JSON File to XML Or text. More on stackoverflow.com
🌐 stackoverflow.com
Converting XML to JSON using Python? - Stack Overflow
That being said, Python's standard library has several modules for parsing XML (including DOM, SAX, and ElementTree). As of Python 2.6, support for converting Python data structures to and from JSON is included in the json module. More on stackoverflow.com
🌐 stackoverflow.com
Online Tool to Convert XML or JSON to Java Pojo Classes
The only "utility" here is the JSON parsing. And I'm sure the xjc people are hard at work on updating it to parse JSON files. If not there are a jillion IDE plugins to get the same thing done. More on reddit.com
🌐 r/java
4
5
February 22, 2014
Online FHIR JSON and XML converter
710 members in the fhir community. FHIR stands for Fast Healthcare Interoperability Resources. It is a standard for exchanging healthcare … More on reddit.com
🌐 r/fhir
3
🌐
Code Beautify
codebeautify.org β€Ί jsontoxml
Best JSON to XML Converter to convert JSON to XML String, File, URL
Best Online JSON to XML Converter, Transformer Online Utility. Load form URL, Download, Save and Share.
🌐
Convertjson
convertjson.com β€Ί json-to-xml.htm
JSON To XML Converter
Use this tool to convert JSON into XML format. New- Now supports JSONLines. Enter your JSON or JSONLines data below and Press the Convert button. The output will display below the Convert button.
🌐
Site24x7
site24x7.com β€Ί tools β€Ί json-to-xml.html
Json to XML Converter: Site24x7 Tools
Site24x7s JSON to XML converter converts your JSON data to its equivalent XML format. The JSON keys are converted to XML elements and JSON values are transformed into element values. The tool supports attributes in XML Element.
Top answer
1 of 5
12

For a simple solution, I recommend Jackson, as it can transform arbitrarily complex JSON into XML with just a few simple lines of code.

import org.codehaus.jackson.map.ObjectMapper;

import com.fasterxml.jackson.xml.XmlMapper;

public class Foo
{
  public String name;
  public Bar bar;

  public static void main(String[] args) throws Exception
  {
    // JSON input: {"name":"FOO","bar":{"id":42}}
    String jsonInput = "{\"name\":\"FOO\",\"bar\":{\"id\":42}}";

    ObjectMapper jsonMapper = new ObjectMapper();
    Foo foo = jsonMapper.readValue(jsonInput, Foo.class);

    XmlMapper xmlMapper = new XmlMapper();
    System.out.println(xmlMapper.writeValueAsString(foo));
    // <Foo xmlns=""><name>FOO</name><bar><id>42</id></bar></Foo>
  }
}

class Bar
{
  public int id;
}

This demo uses Jackson 1.7.7 (the newer 1.7.8 should also work), Jackson XML Databind 0.5.3 (not yet compatible with Jackson 1.8), and Stax2 3.1.1.

2 of 5
6

Here is an example of how you can do this, generating valid XML. I also use the Jackson library in a Maven project.

Maven setup:

<!-- https://mvnrepository.com/artifact/com.fasterxml/jackson-xml-databind -->
    <dependency>
        <groupId>com.fasterxml</groupId>
        <artifactId>jackson-xml-databind</artifactId>
        <version>0.6.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.6</version>
    </dependency>

Here is some Java code that first converts a JSON string to an object and then converts the object with the XMLMapper to XML and also removes any wrong element names. The reason for replacing wrong characters in XML element names is the fact that you can use in JSON element names like $oid with characters not allowed in XML. The Jackson library does not account for that, so I ended up adding some code which removes illegal characters from element names and also the namespace declarations.

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.xml.XmlMapper;

import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Converts JSON to XML and makes sure the resulting XML 
 * does not have invalid element names.
 */
public class JsonToXMLConverter {

    private static final Pattern XML_TAG =
            Pattern.compile("(?m)(?s)(?i)(?<first><(/)?)(?<nonXml>.+?)(?<last>(/)?>)");

    private static final Pattern REMOVE_ILLEGAL_CHARS = 
            Pattern.compile("(i?)([^\\s=\"'a-zA-Z0-9._-])|(xmlns=\"[^\"]*\")");

    private ObjectMapper mapper = new ObjectMapper();

    private XmlMapper xmlMapper = new XmlMapper();

    String convertToXml(Object obj) throws IOException {
        final String s = xmlMapper.writeValueAsString(obj);
        return removeIllegalXmlChars(s);
    }

    private String removeIllegalXmlChars(String s) {
        final Matcher matcher = XML_TAG.matcher(s);
        StringBuffer sb = new StringBuffer();
        while(matcher.find()) {
            String elementName = REMOVE_ILLEGAL_CHARS.matcher(matcher.group("nonXml"))
                    .replaceAll("").trim();
            matcher.appendReplacement(sb, "${first}" + elementName + "${last}");
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    Map<String, Object> convertJson(String json) throws IOException {
        return mapper.readValue(json, new TypeReference<Map<String, Object>>(){});
    }

    public String convertJsonToXml(String json) throws IOException {
        return convertToXml(convertJson(json));
    }
}

Here is a JUnit test for convertJsonToXml:

@Test
void convertJsonToXml() throws IOException, ParserConfigurationException, SAXException {
    try(InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("json/customer_sample.json")) {
        String json = new Scanner(in).useDelimiter("\\Z").next();
        String xml = converter.convertJsonToXml(json);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        Node first = doc.getFirstChild();
        assertNotNull(first);
        assertTrue(first.getChildNodes().getLength() > 0);
    }
}
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί utilities β€Ί xml-to-json-converter
XML to JSON Converter - Free Online Tools %%page%% %%sep%% %%sitename%% - GeeksforGeeks
October 4, 2024 - With the XML to JSON Converter tool, you can effortlessly transform XML data into JSON format, enabling seamless integration with systems requiring JSON input, simplifying data exchange, manipulation, and processing across various platforms ...
🌐
n8n
n8n.io β€Ί tools β€Ί xml-to-json
XML to JSON Converter - Free and Easy Conversion | n8n
Wait for n8n to complete the conversion process, then opt to either download the file or copy the JSON output. Yes, Python offers libraries like xmltodict and xmljson for converting XML to JSON.
🌐
Code Beautify
codebeautify.org β€Ί xmltojson
XML to JSON Converter Online to convert XML to JSON String, URL and File
Best XML to JSON Converter, Transform Online Utility to convert XML to JSON. Paste or Load form URL or File, Download, Save and Share
🌐
Online XML Tools
onlinexmltools.com β€Ί convert-xml-to-json
Convert XML to JSON - Online XML Tools
Simple, free and easy to use online tool that converts XML to JSON. No ads, popups or nonsense, just an XML to JSON converter. Load XML, get JSON.
🌐
ConvertCSV
convertcsv.com β€Ί json-to-csv.htm
JSON To CSV Converter
You can also force double quotes around each field value or it will be determined for you. The output CSV header row is optional. See also CSV to JSON and CSV to GeoJSON Plus Convert JSON to XML, XML to JSON, JSON Lint, JSON Formatter and Analyze JSON Paths at ConvertJSON.com
🌐
Liquid Technologies
liquid-technologies.com β€Ί online-json-to-schema-converter
Free Online JSON to JSON Schema Converter
The Free Community Edition of Liquid Studio comes with an advanced JSON Editor, packed with many helpful features including JSON schema creation. ... Data Security Warning - Tick this box to confirm you understand all data is stored on our servers ...
🌐
Teleport
goteleport.com β€Ί resources β€Ί tools β€Ί json-to-xml-converter
JSON to XML Converter | Instant JSON to XML Conversion | Teleport
Convert JSON data to XML instantly with our free online tool. Simplify your data conversions with fast, accurate processing.
🌐
Drapcode
drapcode.com β€Ί utilities β€Ί xml-to-json-converter
XML to JSON Converter - Transform Structured Data Easily
Convert XML to JSON easily with DrapCode's free online tool. Fast, secure, and efficient. No downloads required.