XML to Json serialization is not mapping the data properly
See original GitHub issueI have some list months in the row tags in a XML format. But while mapping the data to Json it is not mapping properly. Please check below for the reference.
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class XML2Json {
public static void main(String[] args) throws JsonProcessingException, IOException {
String xml = "<root><months><row>Apr 2013</row><row>May 2013</row><row>Jun 2013</row><row>Jul 2013</row><row>Aug 2013</row></months></root>";
XmlMapper xmlMapper = new XmlMapper();
JsonNode node = xmlMapper.readTree(xml.getBytes());
ObjectMapper jsonMapper = new ObjectMapper();
String json = jsonMapper.writeValueAsString(node);
System.out.println(json);
}
}
The output for the above code is:
"months": {
"row": "Aug 2013"
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Jackson XML to JSON converter removes multiple child records
I was able to get the solution to this problem by using org.json API to convert source XML to JSONObject and then to...
Read more >Mapping Between JSON and XML - WCF | Microsoft Learn
On JSON to XML mapping, any escaped characters and characters that are not escaped map correctly to the corresponding [character code].
Read more >How to Convert JSON to XML or XML to JSON in C# - Code ...
This is the recommended way to go if the JSON structure is well-known. This strategy also works for reverse transformation from XML to...
Read more >Don't Directly Serialize Your Classes To JSON or XML
JSON has names and values but a limited type system; in particular, since it has no record definitions (schema), this means that to...
Read more >Solving the XML Problem with Jackson - Stackify
Use Jackson XML module to support for both JSON and XML data ... Note that some of these settings might not work with...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’d like to add that
Does not solve existing problem. We have problem here that might be hard to solve due to technical problems, but take perspective of end-user using this library. I just want to convert XML to JSON, with some constraints, I don’t want creating any POJO, because in most cases I will not know structure of incoming XML. In this case there might be some configurable guess, how it should be mapped. Additionaly I can provide XSD file for well-defined mapping for specific case. I also want to keep root tag from XML in result JSON. I might care for edge cases later, once I can solve my initial problem.
Now, what solution do I have right now, without studying intrinsics of Jackson and this XML extension for it?
I prepared small Kotlin library to overcome problems mentioned in this issue. It offers naive implementation to overcome problem with missing root tag and it maps multiple child elements with the same name into a array.