question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

XML to Json serialization is not mapping the data properly

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Adiqqcommented, Nov 19, 2018

I’d like to add that

Code works exactly as expected; there will not be “fixes”.

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?

1reaction
Adiqqcommented, Nov 21, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found