If you are using Java SE and can't use foreign JARs, and your JSON is always simple as the example you posted, you can parse it. Here's a short program that works for your example (but you will certainly have to adapt it if you have more complex JSON strings with more nesting levels and arrays):
public class SimpleJsonToXml {
public static void main(String[] args) {
String json = "{'sector':'Europe','organization':'Bazz Market Unit UK','companyCode':'UK1_2020','approvalManager':'03000069','managementLevel1':'X11','approvalLimit':'500000','access Category':'FTeam','currency':'USD','comments':'Need this access, so I can do my job properly'}";
String jsonObject = json.replaceAll("\\{(.*)\\}", "$1");
String[] childElements = jsonObject.split("(,)(?=(?:[^\']|\'[^\']*\')*$)");
System.out.println("<root>");
for (String item : childElements) {
System.out.println(makeTag(item));
}
System.out.println("</root>");
}
public static String makeTag(String jsonProperty) {
String[] element = jsonProperty.split("\\:");
String tagName = element[0].replaceAll("[' ]", "");
String tagValue = element[1].replace("'", "");
return " <"+tagName+">"+tagValue+"</"+tagName+">";
}
}
It will print:
<root>
<sector>Europe</sector>
<organization>Bazz Market Unit UK</organization>
<companyCode>UK1_2020</companyCode>
<approvalManager>03000069</approvalManager>
<managementLevel1>X11</managementLevel1>
<approvalLimit>500000</approvalLimit>
<accessCategory>FTeam</accessCategory>
<currency>USD</currency>
<comments>Need this access, so I can do my job properly</comments>
</root>
To convert XML back to JSON you can use the native Java XML tools and parsers (org.w3c.dom
and org.xml.sax
, for example) and won't need any external Jars.
If you are using at least Java EE 7, you can use the parsers in the javax.json package.