2

Background:

  • I'm calling web APIs that are in JSON format and passing them through a data orchestration tool that needs them in XML format.
  • Orchestration tool allows custom Java procedures.

Problem:

  • JSON can contain elements that when converted to XML cause issues. For example twitter handles @john: somevalue is fine for a key in JSON but when converted to XML <@john>somevalue causes the orchestration tool to throw errors.
  • I'm hitting a wide variety of web APIs that change often. I need to be able to convert arbitrary JSON to XML with little to no maintenance.

Research so far:

  • I've found several ways to convert JSON to XML in Java but many of them are for fixed input structures.
  • This StackOverflow post seems like what I want but I'm having issues getting it to work and tracking down all of the JARs required.
  • I've seen some libraries will do some basic character escapes for &, <, >, ' and ". Is there one that is more robust?
5
  • You could use some XML formulation that does not require the translation of JSON property names to XML tags. For example <prop key="@john"> ... </prop> Commented Jan 27, 2014 at 18:59
  • @Henry do you have some documentation on <prop key ="">? Commented Jan 27, 2014 at 19:28
  • Sounds like a simple matter of programming to me. Presuming that there's no need for "the other end" to understand them, you can define your own escape patterns. And if you're careful you can do the escape processing on the JSON string, before it's parsed. Commented Jan 27, 2014 at 20:15
  • Have you tried jettison? At least it can handle the @attribute thingy, (as an XML attribute). Commented Jan 27, 2014 at 22:55
  • Underscore-java library has a static method U.jsonToXml(json) Commented Sep 27, 2023 at 23:38

1 Answer 1

2

I ended up deserializing the JSON and traversing the data by using the following regex to find the nodes and then remove or replace non latin characters.

Regex that grabs JSON node

"(.*?)":
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.