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?
Community
  • 1
  • 1
dreyco676
  • 911
  • 1
  • 8
  • 14
  • You could use some XML formulation that does not require the translation of JSON property names to XML tags. For example ... – Henry Jan 27 '14 at 18:59
  • @Henry do you have some documentation on ? – dreyco676 Jan 27 '14 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. – Hot Licks Jan 27 '14 at 20:15
  • Have you tried [jettison](http://jettison.codehaus.org/)? At least it can handle the `@attribute` thingy, (as an XML attribute). – forty-two Jan 27 '14 at 22:55

1 Answers1

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

"(.*?)":
dreyco676
  • 911
  • 1
  • 8
  • 14