JSON to XML with Jackson
April 10, 2018
I wanted to take an arbitrary JSON string and turn it into the equivalent XML. This can be done using the Jackson library.
String jsonString = "" + "{\n \"_links\": {\n" + " \"individual\": {\n" + " \"href\": \"/individuals/matching\",\n" + " \"name\": \"GET\",\n" + " \"title\": \"Individual Details\"\n" + " },\n" + " \"self\": {\n" + " \"href\": \"/individuals/matching/\"\n" + " }\n" // + " }\n" // + "}"; ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper xmlMapper = new XmlMapper(); JsonNode tree = objectMapper.readTree(jsonString); String jsonAsXml = xmlMapper.writer().withRootName("RootTagName").writeValueAsString(tree);